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>Memeber Login</h2><hr />";
// Show the form
include 'includes/login_form.inc.php';
}
else
{
$username = check_input($_POST['username']);
$password = check_input($_POST['password']);
// Try and login with the given username & pass
$result = user_login($username, $password);
if ($result != 'Correct')
{
echo "<h2>Memeber Login</h2><hr />";
// Reshow the form with the error
$login_error = $result;
include 'includes/login_form.inc.php';
}
else
{
echo "<h2>Welcome ".$_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($username, $password)
{
// Try and get the salt from the database using the username
$query = "select salt from users where username='$username' limit 1";
$result = mysql_query($query);
$user = mysql_fetch_array($result);
// Using the salt, encrypt the given password to see if it
// matches the one in the database
$encrypted_pass = md5(md5($password).$user['salt']);
// Try and get the user using the username & encrypted pass
$query = "select userid, username, user_level from users where username='$username' and password='$encrypted_pass'";
$result = mysql_query($query);
$user = mysql_fetch_array($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($username, $password);[/PHP]
If anyone has any ideas how i would do this i would be very grateful