1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

PHP Login Script


6 of 7

Members Area

 // Connects to your Database 

 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 

 mysql_select_db("Database_Name") or die(mysql_error()); 

 //checks cookies to make sure they are logged in 



 	$username = $_COOKIE['ID_my_site']; 

 	$pass = $_COOKIE['Key_my_site']; 

 	 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 

 	while($info = mysql_fetch_array( $check )) 	 



 //if the cookie has the wrong password, they are taken to the login page 

 		if ($pass != $info['password']) 

 			{ 			header("Location: login.php"); 



 //otherwise they are shown the admin area	 



 			 echo "Admin Area<p>"; 

 echo "Your Content<p>"; 

 echo "<a href=logout.php>Logout</a>"; 






 //if the cookie does not exist, they are taken to the login screen 


 header("Location: login.php"); 



This code checks our cookies to make sure the user is logged in, the same way the login page did. If they are logged in, they are shown the members area. If they are not logged in they are redirected to the login page.

Be sure to download the full code on GitHub: https://github.com/Goatella/Simple-PHP-Login

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Free PHP Login Script - Members' Area

©2014 About.com. All rights reserved.