• All
  • Free Web Hosting
  • Category 2

Archives

gravatar

PHP

 CHECKING LOG-IN INFORMATION IN PHP

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="einstaller"; // Database name
$tbl_name="registration"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$ipadd=$_POST['ipadd'];
$logdate=$_POST['logdate'];
$logtime=$_POST['logtime'];

// this starts the session
 session_start();

 // this sets variables in the session
 $_SESSION['user'] = $myusername;
  $_SESSION['ip'] = $ipadd;


// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE uname='$myusername' and upassword='$mypassword'";
$result=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row


//Log the user <strong class="highlight">in</strong>
 //Assuming the user's username is stored <strong class="highlight">in</strong> $username


if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("ipadd");
session_register("logdate");
session_register("logtime");


header("location: index2.php");
}
else {
echo "<div align='center' style='font-family:verdana;margin-top:280px;background-color:lightblue;color:red;'>";
echo "Sorry! Invalid User Name and Password!";
echo "<p>";
echo "<a href='index.php'><i><style='font-size:12px;'>I want to try Again!</i></a></style>";
echo "</div>";
}
mysql_query("INSERT INTO onlineusers (username,password,ip,date,time) VALUE ('$myusername','$mypassword','$ipadd','$logdate','$logtime')")
?>