Hi,
I created the sign in page with username,passwo rd fields.and along with those fields i put the check box remember and select that one untill we sign out the page the username and password are remains.For that sign page i wrote the code like this [php]<?php
// The database connection.
$con = mysql_connect($ db_host, $db_user, $db_pass);
if(!$con) {
die("Cannot connect. " . mysql_error());
}
$dbselect = mysql_select_db ($db_name);
if(!$dbselect) {
die("Cannot select database " . mysql_error());
}
// Start a session. Session is explained below.
session_start() ;
// Same checking stuff all over again.
if(isset($_POST['submit'])) {
if(empty($_POST['user_name']) || empty($_POST['password'])) {
echo "Sorry, you have to fill in all forms";
exit;
}
// Create the variables again.
$user_name = $_POST['user_name'];
$password = $_POST['password'];
// Store the SQL query inside a variable.
// ONLY the username you have filled in is retrieved from the database.
$query = "SELECT user_name,passw ord
FROM `registered_mem bers`
WHERE user_name='$use r_name'";
$result = mysql_query($qu ery);
if(!$result) {
// Gives an error if the username given does not exist.
// or if something else is wrong.
echo "The query failed " . mysql_error();
} else {
// Now create an object from the data you've retrieved.
$row = mysql_fetch_obj ect($result);
// You've now created an object containing the data.
// You can call data by using -> after $row.
// For example now the password is checked if they're equal.
if($row->password != $password)
{
echo "<center>";
echo "<h2>I am sorry , please enter correct password.</h2>";
echo "<br>";
echo "<a href=forgot.php ><h4> Forgot Password?</h4></a>";
echo "<a href=index.html ><h3> Home </h3></a>";
echo "</center>";
exit;
}
// By storing data inside the $_SESSION superglobal,
// you stay logged in until you close your browser.
$_SESSION['user_name'] = $user_name;
$_SESSION['sid'] = session_id();
// Make it more secure by storing the user's IP address.
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
// Now give the success message.
// $_SESSION['user_name'] should print out your username.
}
}
if(isset($_POST['remember'])){
echo "hello";
setcookie("cook name", $_SESSION['user_name'], time()+60*60*24 *100, "/");
// ae_put_cookie(' cookname', $_SESSION['user_name'], "forever");
echo $_COOKIE["user_name"];
// ae_put_cookie(' cookpass', $_POST['password'], "forever");
setcookie("cook pass", $_POST['password'], time()+60*60*24 *100, "/");
}
echo "<br><b> & nbsp; &nbs p;";
echo "Welcome " . $_SESSION['user_name'];
echo "</b>";
include("login1 .php");
?>[/php]In that remember field can't work properly.plz tell that what's the problem in that code.
I created the sign in page with username,passwo rd fields.and along with those fields i put the check box remember and select that one untill we sign out the page the username and password are remains.For that sign page i wrote the code like this [php]<?php
// The database connection.
$con = mysql_connect($ db_host, $db_user, $db_pass);
if(!$con) {
die("Cannot connect. " . mysql_error());
}
$dbselect = mysql_select_db ($db_name);
if(!$dbselect) {
die("Cannot select database " . mysql_error());
}
// Start a session. Session is explained below.
session_start() ;
// Same checking stuff all over again.
if(isset($_POST['submit'])) {
if(empty($_POST['user_name']) || empty($_POST['password'])) {
echo "Sorry, you have to fill in all forms";
exit;
}
// Create the variables again.
$user_name = $_POST['user_name'];
$password = $_POST['password'];
// Store the SQL query inside a variable.
// ONLY the username you have filled in is retrieved from the database.
$query = "SELECT user_name,passw ord
FROM `registered_mem bers`
WHERE user_name='$use r_name'";
$result = mysql_query($qu ery);
if(!$result) {
// Gives an error if the username given does not exist.
// or if something else is wrong.
echo "The query failed " . mysql_error();
} else {
// Now create an object from the data you've retrieved.
$row = mysql_fetch_obj ect($result);
// You've now created an object containing the data.
// You can call data by using -> after $row.
// For example now the password is checked if they're equal.
if($row->password != $password)
{
echo "<center>";
echo "<h2>I am sorry , please enter correct password.</h2>";
echo "<br>";
echo "<a href=forgot.php ><h4> Forgot Password?</h4></a>";
echo "<a href=index.html ><h3> Home </h3></a>";
echo "</center>";
exit;
}
// By storing data inside the $_SESSION superglobal,
// you stay logged in until you close your browser.
$_SESSION['user_name'] = $user_name;
$_SESSION['sid'] = session_id();
// Make it more secure by storing the user's IP address.
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
// Now give the success message.
// $_SESSION['user_name'] should print out your username.
}
}
if(isset($_POST['remember'])){
echo "hello";
setcookie("cook name", $_SESSION['user_name'], time()+60*60*24 *100, "/");
// ae_put_cookie(' cookname', $_SESSION['user_name'], "forever");
echo $_COOKIE["user_name"];
// ae_put_cookie(' cookpass', $_POST['password'], "forever");
setcookie("cook pass", $_POST['password'], time()+60*60*24 *100, "/");
}
echo "<br><b> & nbsp; &nbs p;";
echo "Welcome " . $_SESSION['user_name'];
echo "</b>";
include("login1 .php");
?>[/php]In that remember field can't work properly.plz tell that what's the problem in that code.
Comment