Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Created September 18, 2016 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anan5a/8d4e02154e00c2926eecc45f5a45d517 to your computer and use it in GitHub Desktop.
Save Anan5a/8d4e02154e00c2926eecc45f5a45d517 to your computer and use it in GitHub Desktop.
<?php
/**
*
*/
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="student"; // Table name
// Connect to server and select databse.
@$con = new mysqli($host, $username, $password,$db_name);
if($con->errno)
{
echo "Error encountered! ".$con->error;
exit;
//exit script.
}
// username and password sent from form
$myusername=isset($_POST['username'])? stripslashes($_POST['username']):'' ;
$mypassword=isset($_POST['password'])? stripslashes($_POST['password']):'' ;
if($mypassword&&$myusername!=null){
//if the variables are not null
// To protect MySQL injection (more detail about MySQL injection)
$myusername = $con->escape_string($myusername);
$mypassword = $con->escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myus
ername' and password='$mypassword'";
$result=$con->query($sql);
// Mysql_num_row is counting table row
// If result matched $myusername and $mypassword, table row must be 1 row
if($result->num_rows==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
}else{
echo "Submit please the form.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment