How to change the login hyper link to logout when successfully logged in

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adrock952
    New Member
    • Aug 2007
    • 8

    How to change the login hyper link to logout when successfully logged in

    I have a link on my site which obviously says "Login" where users log in.

    I would like that link to be changed to "Logout" when the user has successfully logged in and the session has been created and when the user logs out, i would like the link changed back to "Login" without having to refresh the page.

    here is my login page
    [PHP]<?php

    if (is_authed_user ())
    {
    print ('You are already logged in, <a href="index.php ">click here</a> to go back.');
    }
    else
    {

    if (!isset($_POST['submit']))
    {
    echo "<h2>Memebe r Login</h2><hr />";
    // Show the form
    include 'includes/login_form.inc. php';
    }
    else
    {
    $username = check_input($_P OST['username']);
    $password = check_input($_P OST['password']);

    // Try and login with the given username & pass
    $result = user_login($use rname, $password);

    if ($result != 'Correct')
    {
    echo "<h2>Memebe r Login</h2><hr />";
    // Reshow the form with the error
    $login_error = $result;
    include 'includes/login_form.inc. php';
    }
    else
    {
    echo "<h2>Welcom e ".$_SESSION['username']."</h2><hr />";
    echo 'Thank you for logging in, <a href="index.php ">click here</a> to go back.';
    }
    }
    }
    ?>[/PHP] and the function to log the user in if you need it
    [PHP]function user_login($use rname, $password)
    {
    // Try and get the salt from the database using the username
    $query = "select salt from users where username='$user name' limit 1";
    $result = mysql_query($qu ery);
    $user = mysql_fetch_arr ay($result);

    // Using the salt, encrypt the given password to see if it
    // matches the one in the database
    $encrypted_pass = md5(md5($passwo rd).$user['salt']);

    // Try and get the user using the username & encrypted pass
    $query = "select userid, username, user_level from users where username='$user name' and password='$encr ypted_pass'";
    $result = mysql_query($qu ery);
    $user = mysql_fetch_arr ay($result);
    $numrows = mysql_num_rows( $result);

    $userid = $user['userid'];
    $user_level = $user['user_level'];

    // Now encrypt the data to be stored in the session
    $encrypted_id = md5($user['userid']);
    $encrypted_name = md5($user['username']);
    $encrypted_user = md5($user['user_level']);

    // Store the data in the session
    $_SESSION['userid'] = $userid;
    $_SESSION['username'] = $username;
    $_SESSION['user_level'] = $user_level;
    $_SESSION['encrypted_id'] = $encrypted_id;
    $_SESSION['encrypted_name '] = $encrypted_name ;
    $_SESSION['encrypted_user '] = $encrypted_user ;


    if ($numrows == 1)
    {
    return 'Correct';
    }
    else
    {
    return false;
    }
    }[/PHP]

    all i have for the logout page is a simple session_unset() and session_destroy ()

    This seems such a simple task but don't know where to start.

    I presume the ajax would go after this in the login form to change the link from login to logout
    [PHP]$result = user_login($use rname, $password);[/PHP]
    If anyone has any ideas how i would do this i would be very grateful
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Give the link an id. When the user has successfully logged in, i.e. readyState is 4 and you've validated that it's correct, change the link text: document.getEle mentById(linkID ).innerHTML="Lo gout".

    Comment

    Working...