By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

problem with sign in page

P: 2
Hi,
I created the sign in page with username,password 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,password
FROM `registered_members`
WHERE user_name='$user_name'";
$result = mysql_query($query);
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_object($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("cookname", $_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("cookpass", $_POST['password'], time()+60*60*24*100, "/");
}

echo "<br><b>&nbsp;&nbsp;&nbsp;&nbsp;";
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.
Apr 23 '08 #1
Share this Question
Share on Google+
3 Replies


Markus
Expert 5K+
P: 6,050
[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,password
FROM `registered_members`
WHERE user_name='$user_name'";

$result = mysql_query($query);
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_object($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("cookname", $_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("cookpass", $_POST['password'], time()+60*60*24*100, "/");
}

echo "<br><b>&nbsp;&nbsp;&nbsp;&nbsp;";
echo "Welcome " . $_SESSION['user_name'];
echo "</b>";
include("login1.php");

?>
[/php]
Please use code tags.
Apr 23 '08 #2

Markus
Expert 5K+
P: 6,050
lines 68 and 70 should be something like
[php]
setcookie("remember_me", $_SESSION['username'], time() + ... ");
echo $_COOKIE["remember_me"];
[/php]
Apr 23 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
WARNING:
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Apr 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.