hi ive decided to use sessions breifly in my login script.

basic design -

input user, pass. submit the form and as output a logout text will appear with the users name under a session variable.

when i refresh the page the login text form will not apear because the user is still in that session. YAY all going to plan.

but.. when i click a href link the session seems to end and the form is shown once again. even though the user may have logged in on another page !

session_start()
$SESSION['reg_user'] = $POST['username'];

session is not ended

    And we're to assume that the linked page also contains "session_start();" ??

      Try this for a great approach to session management, including an access control script that you include on every page. It checks if the user has logged in and maintains the session variables for you. Whatever page the users first open they will be prompted for a login, usefull for bookmarks to restricted pages.

      http://www.sitepoint.com/print/users-php-sessions-mysql

        hey, thanks for the replys, the login form section works, but i just need to find a way to end a session via ahref link

        I was thinkin on the lines of a $_SERVER['PHP_SELF'] in the link then relate to some sort of unique data relating to the session user.

        im stuck for ideas but im just looking for an edge to work with so the session can be ended for that user.

        thanks for the replys

        any replays will be great thanks.

          [font=trebuchet ms]Simwiz, is your only problem ending a session now (logging out), or are you still having problems with your first question? Your last reply was not very clear to me.[/font]

            hi again if you read the above you will find this

            the login form section works, but i just need to find a way to end a session via ahref link

            YUP the session works i tested the var on other pages to see if the session var held the data it did 😃. Also the login form problem is sorted.

            Only problem now is logging out via a link as stated in last post.

            be brillaint if you could help me with a lo out link which ends session

            thanks

              [font=trebuchet ms]Link to logout.php, which will look something like the following.[/font]

              <?php
               # If user is logged in...
               if(isset($_SESSION['reg_user'])){
                 session_unset(); #log him out
                 echo "<h1>Logged out</h1>\n"
                         ."<p>You have been logged out successfully.</p>";
                 exit; # stop any other code from processing
              } else { # if he isn't logged in
                 header("Location: index.php"); # redirect to index.php
                 exit; # make sure no other code is processed
              }
              ?>
              

                oh of course cheers !!!

                eh so when the page loads this will be executed, great stuff !!!

                only problem is i would love a header redirect but html is ouputted before hand :/

                for example i have a image above the login form so the header code will be below that in a table

                dont think there is away to move this because then the html code echoed inside the php will be moved

                thanks !

                  [font=trebuchet ms]Actually there is. Put this at the very beginning.[/font]

                  <?php
                   # If user is logged in...
                   if(isset($_SESSION['reg_user'])){
                     session_unset(); #log him out
                  } else { # if he isn't logged in
                     header("Location: index.php"); # redirect to index.php
                     exit; # make sure no other code is processed
                  }
                  ?>
                  

                  [font=trebuchet ms]Since there is an exit command, the following code will not appear if the user is not logged in. If he is, though, there is no exit command, hence he will see the message. The following code goes where you want the message to appear.[/font]

                     echo "<h1>Logged out</h1>\n"
                             ."<p>You have been logged out successfully.</p>";
                  

                    great stuff

                    thanks.

                    so you could actually make a tag such as if logged out then $tag = 1

                    then where you want a message to appear if $tag = 1 then echo message.

                    i understand where you r coming from but the message will have to be later on in the code between html tables

                    this would work perfect for auto log out with no output to the user

                      [font=trebuchet ms]That's exactly right - you can create a flag variable to determine whether or not to output a message (or to determine which message to output, by using an array). Whatever floats your boat.[/font]

                        lol i was mistaken with the tag which is used in pascal DOH !!!

                        I was on the right lines just didnt call it properly

                        cheers for the help been very informative.

                        example code.

                        $msg_logout = 'you are logged out MUWHAHHAHA';

                        later in html code --

                        echo $msg_logout;

                        so if they are already logged out will the var never get execuated or assigned ?

                        thanks again, il be doing my self a database related site. my login script works just slowly going through each stage.

                        possibly when getting round to it more validation on registation page

                        example age - twenty

                        int only instead of string. but i guess the code will do for now

                        thanks again

                          Originally posted by simwiz
                          so if they are already logged out will the var never get execuated or assigned ?

                          [font=trebuchet ms]Correct. It is because the exit command, which stops any further PHP from processing. It only exits, though, when the user is not logged in. This is because it redirects him.[/font]

                            the code actually is so ideal for what im doing.

                            I have if statements relating to the session var soon this is ended so is the session related code 😃

                            cheers

                            now that ive had a proper look at it 😉

                              [font=trebuchet ms]Happy to help.[/font]

                                Write a Reply...