SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Logout Link

  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logout Link

    I currently have the following code that logs a user onto the system, using sessions. On the members page, how do I create a logout link, that kills the login session so the user will be directed to the login page if he/she is not currently logged in?

    Code PHP:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
     
    mysql_select_db("Users", $con);
     
    $username = $_POST['username'];
    $password = $_POST['password'];
     
    $sql = "SELECT id FROM records WHERE username = '" . mysql_real_escape_string($username) . "' AND password = '" . mysql_real_escape_string($password) . "'";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) == 0)
    {
      echo "Username and password combination not found.";
    }
    else
    {
      $row = mysql_fetch_array($result);
      $_SESSION['logged_in'] = 1;
      $_SESSION['user_id'] = $row['id'];
      header("Location: http://www.example.com/member-area.php");
    } 
    ?>

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey View Post
    How do I put that in a link?
    How does it know which session to destroy?

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your logout link points to a script that runs session_destroy, exact same way you log your users in or run any other PHP code.

    It will destroy the user's session. You may as well ask how PHP knows which session to use when you do anything with sessions - there's a cookie on the user's computer that contains a session ID which ties that user to a particular session.
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Logout Link

    Can you show me how to create a Logout link, as I'm new to PHP, and didn't create the code found within Post 1


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •