Johnny G Junior's NoteBook Treasures: Simple Username and Password Login PHP/MYSQL

Sunday, April 19, 2009

Simple Username and Password Login PHP/MYSQL



I place NOTES in the actual script.... notes start with /// which are disabled in the script


 session_start();  
 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');   
 header('Cache-Control: no-store, no-cache, must-revalidate');   
 header('Cache-Control: post-check=0, pre-check=0', FALSE);   
 header('Pragma: no-cache');  
   
 /// NOTE FOR YOU - write a connection to your mysql   
   
 $user = $_POST["username"];  
 $pass = $_POST["password"];  
 $table = "WRITE TABLE HERE";  
 $field = "WRITE FIELD HERE";  
   
   
   
 if(isset($_POST['op'])) {  /// if the form is submitted  
   
   //// if Username is NOT Empty and Password is NOT EMPTY  
   if (!empty($_POST['username']) && !empty($_POST['password'])){  
   
 /// create an SQL variable to check username and password  
 $sql = "SELECT $field FROM $table WHERE username='$user' AND password='$pass'";  
   
 $result = mysql_query($sql);  ///  QUERY  
 $num = mysql_num_rows($result); //// evaluate the # of entries from $result  
   
   
 if ($num == 0 || $num > 1) { //// if $num = 0 or is greater than 1   
   
 //// if posted username is NOT equal to username OR posted password is NOT equal..  
   
 if ($_POST['username'] != 'username' || $_POST['password'] !='password'){  
   
 $msg = "Bad Login - Try again"; /// create a variable called $msg to store message  
 $_SESSION['valid'] = 'invalid'; // SESSION = invalid  
 }  
 } else { /// else if the post IS equal  
 $_SESSION['valid'] = 'yes'; // SESSION = yes  
 header('location:http://www.YOURLOCATION.com'); // change location  
 exit; // exit the rest of the script  
 }  
 } else {  //// if you didnt enter in anything in either the user or password field  
 $msg = "Please enter username AND password";  
 }  
 }  
 ?>  



Lets make a form now in the same PHP file.. QUICK NOTE.. this form is missing quotes because i could not get this to publish without some modification. Create a form and add the php script and hidden elements just like the form below.

Username:
Password:

1 comment:

manish said...

php code for photo tagging with demo and database

http://www.lessonwithdemo.com/205/ajax/photo-tagging-like-facebook-without-page-refresh-with-ajax-without-jquery.php/