SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

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>";
?>



Further readings
PHP Session: Creating , using destroying Session variables
Online membership management script
Posting Activation key for lost encrypted password to the email address
Creating a signup page and storing member details in MySQL
Storing signup data in a table
Collecting the data if register global is off for signup script
Changing or updating password by the logged in member
Forgot password feature in member signup script
Basic login form
Login script to authenticate user from a mysql table data
Checking the status of user for login or logged out
Updating member profile inside member area
Finding out who are online









Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.