473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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>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
Feb 15 '08 #1
1 3259
acoder
16,027 Expert Mod 8TB
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.getElementById(linkID).innerHTML="Logout" .
Feb 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Shakun | last post by:
Hi All, This is my 1st posting to this group. Can any1 help me with the "Remember Me" which is there in a login form. Im pasting the code below. Im not able to set a cookie.. Thanks, Shakun...
3
by: Jennifer.Berube | last post by:
okay...so I got this login script and I edited it all and it seems to run fine...IE it listens to the script as far as permissions go when I place a restriction on a page and when you login it...
0
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a two pages in the locations ~/Folder1/LoggedIn.aspx and ~/Folder2/Services.aspx. Both folders have a web.config file that redirects unauthenticated users to ~/Login.aspx. When a user is...
0
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a two pages in the locations ~/Folder1/LoggedIn.aspx and ~/Folder2/Services.aspx. Both folders have a web.config file that redirects unauthenticated users to ~/Login.aspx. When a user is...
3
by: burrashiva | last post by:
:( ::) Hello there, I recently started Web Designing with PHP, Javascript and AJAX. I am facing a problem which I will try to explain: If I am successful in conveying the problem to you...
1
by: webandwe | last post by:
Hi, Can somebody please show me how to change the connection so I can make it work with my MYSQL database... I just need this login to work then I'm done wiht my project. I don't know what is...
10
by: DavidPr | last post by:
When I logout as one user and log in under a different user, it opens with the last user's information. User 1 - Unsername: Davey Jones User 2 - Unsername: David Smith I log out from Davey...
9
by: adweaver | last post by:
Hello All, I'm new to the world of php. I've just had a site designed for me by a company, and I'm now trying to manage and grow it, so it will suit my needs. The site was built in a folder...
22
by: macdalor | last post by:
HI, I hope someone can help. I'm trying to show the username of the logged in user on this menu.inc page and certain pages only when logged in. The result I have with the code below is no...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.