SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

PHP Login and logout script and example

Depending on the visitors session condition whether logged in or not we can display different messages to the members.We should show an welcome messages to a logged in user like "Welcome john" with a logout button. Same way we if the member has not logged in then we can show login input box in that place or show the link to login page.

We can check the visitors session variable and display a login or logout link using one PHP IF condition. This is one example of php if and see detail Login script for full code. We will be using PHP isset function to check the existence of session variable.



Read more on PHP Session variables here

In PHP 5 and above the session checking has to be done like this

if(!isset($_SESSION['userid'])){
// session not logged in so display form

}else{
// session logged
}


Here is the code 

<table width="98%" border="0" cellspacing="2" cellpadding="2">

<?
if(isset($session[userid])){  // Member is logged in so we have to display welcome message with userid and one logout link
echo "<tr><td align=center'><strong><font color='#FFFFFF'>Welcome $session[userid]</font></strong></td></tr>
<tr> <td><font color='#FFFFFF'><strong><a href='logout.php'>LOGOUT</a></strong></font></td></tr>
";

}else {  // Member has not logged in so we can display the login form allowing member to login with user id and password

echo "<form name='login' method='post' action='loginck.php' style='margin:0px'>
<tr><td width='37%'><strong><font color='#FFFFFF'>Username</font></strong></td>
<td width='58%'><input name='id' type='text' class='field' id='username22' size='14'></td>
</tr>

<tr><td><strong><font color='#FFFFFF'>Password</font></strong></td>
<td><input name='pw' type='password' class='field' id='password227' size='14'></td>
</tr>

<tr><td>&nbsp;<a href='forgot.php'>Forgot password</a></td>
<td valign='top'><input name='imageField2' type='image' src='images/login.gif' width='48' height='18' border='0'></td>
</tr></form>";
Related Tutorial
Forgot password script
PHP Signup script
PHP Login script

}    // End of else condtiion

echo "</table>";
?>







Join Our Email List
Email:  
For Email Newsletters you can trust
Sign up
PHP Sections