Skip to content

Instantly share code, notes, and snippets.

@smonteverdi
Created March 7, 2012 15:55
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save smonteverdi/1993937 to your computer and use it in GitHub Desktop.
Save smonteverdi/1993937 to your computer and use it in GitHub Desktop.
PHP: Login Form
login_page.php
<form action="verify.php" method="post">
User Name:<br>
<input type="text" name="username"><br><br>
Password:<br>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Login">
</form>
verify.php
<?php
if(isset($_POST['submit'])){
$dbHost = "localhost"; //Location Of Database usually its localhost
$dbUser = "xxxx"; //Database User Name
$dbPass = "xxxxxx"; //Database Password
$dbDatabase = "db_name"; //Database Name
$db = mysql_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database.");
//Connect to the databasse
mysql_select_db($dbDatabase, $db)or die("Couldn't select the database.");
//Selects the database
/*
The Above code can be in a different file, then you can place include'filename.php'; instead.
*/
//Lets search the databse for the user name and password
//Choose some sort of password encryption, I choose sha256
//Password function (Not In all versions of MySQL).
$usr = mysql_real_escape_string($_POST['username']);
$pas = hash('sha256', mysql_real_escape_string($_POST['password']));
$sql = mysql_query("SELECT * FROM users_table
WHERE username='$usr' AND
password='$pas'
LIMIT 1");
if(mysql_num_rows($sql) == 1){
$row = mysql_fetch_array($sql);
session_start();
$_SESSION['username'] = $row['username'];
$_SESSION['fname'] = $row['first_name'];
$_SESSION['lname'] = $row['last_name'];
$_SESSION['logged'] = TRUE;
header("Location: users_page.php"); // Modify to go to the page you would like
exit;
}else{
header("Location: login_page.php");
exit;
}
}else{ //If the form button wasn't submitted go to the index page, or login page
header("Location: index.php");
exit;
}
?>
users_page.php
<?php
session_start();
if(!$_SESSION['logged']){
header("Location: login_page.php");
exit;
}
echo 'Welcome, '.$_SESSION['username'];
?>
@SaifShovon
Copy link

Thank You

@havugimanaremy
Copy link

thanks

@mic7x4
Copy link

mic7x4 commented Jun 23, 2018

@smonteverdi Thanks bruh
u saved me thoo

@technosmarter
Copy link

technosmarter commented Aug 21, 2018

The session is very essential for all web application.
First of all, understand login without session -
Simple Login demo
After understood login. Now you have to need to understand login with the session.

Login with session complete system-Session start-destroy-timing

@sabiyaskh98
Copy link

thanks

@alinabolotbekova
Copy link

danke

@Vanitah
Copy link

Vanitah commented Nov 21, 2018

thank you.

@sanjeevminati
Copy link

can u plzz say why u didnt use cookie here

Copy link

ghost commented Dec 9, 2018

It doesnt work can someone please help me. I can connect to database and access the login page. but when I enter the correct credentials it doesnt work...

@OxydeTheItalian
Copy link

OxydeTheItalian commented Dec 17, 2018

It doesnt work can someone please help me. I can connect to database and access the login page. but when I enter the correct credentials it doesnt work...

The code above is not working because there's some HTML before the header.
As you can read here: http://php.net/manual/en/function.header.php The header won't accept anything before the PHP. If you move the FORM after the PHP will do.
Note: For some x reason the header works if you are doing it on a localhost (like using xampp or anything like that), however it will not redirect you if your login form is online. This depend on the .ini file. Not sure if you can change that. I usually put all the HTML after my redirect in PHP (aka header).

@MwauraMike
Copy link

Woooow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment