Learn how to use sessions in PHP to create a simple login that remembers a user.
ID: 3827822 • Letter: L
Question
Learn how to use sessions in PHP to create a simple login that remembers a user. For this assignment you should make a login form containing a username field and a password field. The form should use the POST method (rather than GET) to send the information to a PHP document that compares the username to the stored username and the password to the salted and hashed password. Include the following 3 lines as the first three lines of the php file: DEFINE("USR", "jack"); DEFINE ("SALT", "4;lkasdf1"); DEFINE ("PASSWD", sha1 ("54321").SALT); (Learn about define here) Use these lines to define the username, salt and password that you will compare with.Explanation / Answer
Ans::
The below is the login form with remember me feature.Thank you.
<html>
<head>
<h1> login form with remember feature </h1>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='3' align='center'>
<tr><td>User_name:</td><td><input type='text' name='usr'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember_me</span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
define("usr","jack")
define("salt","4;lkasdf1")
define("passwd",sha1("54321".salt)
session_start();
if(isset($_COOKIE['usr']) && isset($_COOKIE['pwd']))
{
header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$name=$_POST['usr'];
$pwd=$_POST['pwd'];
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from login where name='".$usr."' and password='".$pwd."' ");
$res=mysql_fetch_row($query);
if($res)
{
if(isset($_POST['remember']))
{
// set cookie , time
setcookie('name',$name, time() + (60*60*24*1));
setcookie('pwd',$pwd, time() + (60*60*24*1));
}
// session
$_SESSION['name']=$usr;
// redirects to welcome.php
header('location:welcome.php');
}
//else
else
{
// enter correct credentials
echo'The enteredd username or password is invalid';
}
}
else
{
echo 'Enter both the user_name & password';
}
}
?>
</body>
</html>
//// styles ////
<style type="text/css">
input{
border:2px solid olive;
border-radius:4px;
}
h1{
color:orange;
font-size:21px;
text-align:center;
}
span{
color:yellow;
}
</style>
//////////// end ////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.