06-27-2008, 12:18 AM
|
#20 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
now i have a new problem i login with right pass and username, it all works sends me to index if the user is logged in but when i try to logout..
it wont logout :S
THIS IS ON index:
PHP Code:
session_start(); // Starts the session.
if ($_SESSION[‘logged’] != 1) { // There was no session found!
header("Location: users.php"); // Goes to login page.
exit(); // Stops the rest of the script.
}
echo "This is the main page!";
LOGOUT page,
PHP Code:
<?php
session_unset(); // Destroys the session.
header("Location: users.php"); // Goes back to login.
?>
and this is the users page
PHP Code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
include("db_connect.php");
session_start(); // Starts the session.
if ($_SESSION[‘logged’] == 1) { // User is already logged in.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else {
if ( ! isset($username))
{
$username = '';
}
if ( ! isset($password))
{
$password = '';
}
$szForm = <<<FORM
<form action="users.php" name="login" method="post">
<table>
<tr><td>username</td>
<td><input type="text" name="username" value="{$username}" /></td>
</tr>
<tr><td>password</td>
<td><input type="password" name="password" value="{$password}" /></td>
</tr>
<tr><td colspan="2"><input type='submit' name='login' value='login' /></td></tr>
</table>
</form>
FORM;
echo $szForm;
$password = mysql_real_escape_string($_POST['password']);
$username = mysql_real_escape_string($_POST['username']);
$q = mysql_query("SELECT * FROM users WHERE username = '$username'
AND password = '$password'") or die (mysql_error()); // mySQL query
$r = mysql_num_rows($q); // Checks to see if anything is in the db.
if ($r == 1) { // There is something in the db. The username/password match up.
$_SESSION[‘logged’] = 1; // Sets the session.
header("Location: index.php"); // Goes to main page.
exit(); // Stops the rest of the script.
} else { // Invalid username/password.
exit("Incorrect username/password!"); // Stops the script with an error message.
}
}
?>
|
|
|
|