login/logout question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

login/logout question

Post by irealms »

at present i have the following script

<?
if ($userid && $password)
{
// if the user has just tried to log in
$db_conn = mysql_connect("localhost", "cadmin", "cpass");
mysql_select_db("crimson", $db_conn);
$query = "select * from user "
."where username='$userid' "
." and passwd='$password' ";
$result = mysql_query($query, $db_conn);
if (mysql_num_rows($result) >0 )
{
// if they are in the database register the user id
$valid_user = $userid;
session_register("valid_user");
}
}

if (session_is_registered("valid_user"))
{
echo "You are logged in as: $valid_user <br>";
echo "<a href=\"index.php?page=logout\">Log out</a><br>";

}
else
{
if (isset($userid))
{
// if they've tried and failed to log in
echo "Could not log you in";
}
else
{
// they have not tried to log in yet or have logged out
echo "<div class=\"log\">You are not logged in.</div>";
}

// provide form to log in
echo "<form method=post action=\"index.php?page=authmain\">";
echo "<table class=\"log\">";
echo "<td>Userid:</td>";
echo "<td><input type=text name=userid></td>";
echo "<td>Password:</td>";
echo "<td><input type=password name=password></td>";
echo "<td colspan=2 align=center>";
echo "<input type=submit value=\"Log in\"></td></tr>";
echo "</table></form>";
}

?>

and this as logout

<?
$old_user = $valid_user; //test if user *were* logged in
$result = session_unregister("valid_user");
session_destroy();
?>
Log out
<?
if (!empty($old_user))
{
if ($result)
{
// if user was logged in and are not logged out
echo "You are now logged out.";
}
else
{
// user was logged in and could not be logged out
echo "Could not log you out.";
}
}
else
{
// not logged in and accessed this page
echo "You were not logged in.";
}
?>

I'm trying to make it so when you click log out the text saying you are logged in is replaced with "you are logged out"

at the moment the text pops up saying your logged out but the other text saying your logged in is still there.

site is http://www.irealms.co.uk/crimson
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Check to see if the session user is there, if so display the login sentence, if not, display the invalid login sentence.

http://phpcomplete.com/tutorials.php?id ... adTutorial

Code: Select all

<?php
     session_start();
	if(empty($_SESSION['username'])) {
		die('An error has ocurred. It may be that you have not logged in, or that
your session has expired.
			Please try <a href="login.php">logging in</a> again or contact the 
			<a href="mailto:admin@example.com">system administrator</a>');
	}
?>
Post Reply