php log in and log out procedure?

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
djcrisp
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 6:25 pm

php log in and log out procedure?

Post by djcrisp »

anyone know how can i make sure that the user is logged in? and how do i create a logout procedure? also if user hits the back button, it should log them out. how can i go about doing this? any modifications, any help is greatly appreciated.

thanks in advance :-)





Please enter username and password:</br>
<form method="post" action="asif.php">
Username: <input type="text" name="username" size="20"></br>
Password: <input type="password" name="password" size="20"></br>

<input type="Submit" value="Submit">
</form>
</body>
</html>

<?php }else{
$username=$_POST["username"];
$password=$_POST["password"];
$email=$_POST["email"];
session_start();
if ($username=="test" AND $password=="test"){ $permission="yes";}
if ($username=="test" AND $password=="school"){ $permission="yes";}

$username=$_POST["username"];
session_register("permission");
session_register("username");

if ($permission=="yes"){
?>



<?php }else{ ?>

Error in username or password
<?php } ?>
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: php log in and log out procedure?

Post by watson516 »

Next time you post code, instead of increasing the size, there are these little wonderful tags supplied by the forum. [code =php][/code ]

As for the login/logout. You could use SESSION variables to store login details after successful login. When the user logs out, you can just change the values of the session variables. As for the back button, you could probably store the current page in a session variable and then check it whenever a page loads. If you have links on the logged in page, you could put some sort of flag in the url unless you only have a single page that the user stays logged in.

Code: Select all

session_start();
...
if($_SESSION['page']==$_SERVER['PHP_SELF'])
{
   if(userLogin($username,$password)
   {
       $_SESSION['user']=$username;
       $_SESSION['page']=$_SERVER['PHP_SELF'];
       header('Location: admin.php');
   }else{
       header('Location: login.php?error=fail');
   }
}else{
   $_SESSION['user']="GUEST";
   $_SESSION['page']=$_SERVER['PHP_SELF'];
}

Something like that maybe?
Post Reply