Log in

View Full Version : plz help me



Smile
05-29-2008, 12:32 PM
I need code of login and remember me with cookies in php without using database because I have one user to login.

I used many of codes but I had problem in method setcookie:(

this is example of codes but can one tell is it true or not????

this error appear when run it
Warning: Cannot modify header information - headers already sent by ......


index.php:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<link rel = "stylesheet" type = "text/css"
href="../externalCSS.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';
if (isset($_COOKIE['username']) && isset($_COOKIE['password']))
{

if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass)))
{
echo ('Location: newlogin.php');
}
else
{
echo 'Welcome back ' . $_COOKIE['username'];
}

}
else
{
echo ('Location: newlogin.php');
}
?>
<form method="post" name="cookie" action="newlogin.php">
<table align="center">
<tr>
<td ><sub><img src="../design images/logo.jpg" width="76" height="87"/></sub><b>Four Seasons</b></td>
</tr>

<tr><td><hr color="#CC0033"/></td></tr>
<tr><td align="center">
<table style= "border-style:solid;border-color:#CC0033;border-width:thin;align:center">
<tr><td><label for="username">Username: </td><td><input name="username" t id="username" type="text" size="25" /></label></td></tr>
<tr><td><label for="password">Password: </td><td><input name="password" id="password" type="password" size="25" /></label></td></tr>
<tr><td><input type="checkbox" name="setcookie" value="setcookie" /> Remember Me</td></tr>
<tr align="center"><td colspan="2"><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset"/></td></tr></table>
</td></tr>
</form>

<!--The follwing row is just for pushing the footer to the bottom-->
<tr height="90"><td></td></tr>
<tr><td><br /><hr color="#CC0033"/></td></tr>
<tr><td align="center"><p class="smallFont">King Saud University. All rights reserved,2008</p></td></tr>
</table>
</body>
</html>




newlogin.php:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';
extract($_POST);
if (isset($username) && isset($password)) {

if (($username == $user) && ($password == $pass)) {

if (isset($rememberme)) {
/* Set cookie to last 1 year */
setcookie('username', $username, time()+60*60*24*365, '', '');
setcookie('password', md5($password), time()+60*60*24*365, '', '');

} else {
/* Cookie expires when browser closes */
setcookie('username', $username, false, '', '');
setcookie('password', md5($password), false, '', '');
}
print ("<h1>Registered!</h1><p>Thank you <b>
<?php echo $username; ?> </b>, your information has been added to the database, you may now <a href='adminHome.htm' title='Login'>log in</a>.</p>
");

} else {
print ("Username/Password Invalid");
die();
}

} else {
print ("You must supply a username and password.");
die();
}
?>
</body>
</html>

Nile
05-29-2008, 10:36 PM
I think that the echo ('Location: ...'); has to be in the <head> part of the document. And I think that it should be: header ('Location: ...'); from my knowledge at least. Also same with the cookies, in the <head> part of the document.
Also just a tip:

print ("Username/Password Invalid");
die();
Make it this:


die("Username/Password Invalid");

Same for the print() below it.

thetestingsite
05-29-2008, 10:45 PM
Nile is partly correct. The setcookie and header parts need to be placed before anything is outputted to the browser. Basically, you would need to do something like the following:

index.php


<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {

if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
header('Location: newlogin.php');
}
else {
$welcome = 'Welcome back ' . $_COOKIE['username'];
}

}

else {
header('Location: newlogin.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<link rel = "stylesheet" type = "text/css"
href="../externalCSS.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?php echo $welcome; ?>
<form method="post" name="cookie" action="newlogin.php">
<table align="center">
<tr>
<td ><sub><img src="../design images/logo.jpg" width="76" height="87"/></sub><b>Four Seasons</b></td>
</tr>

<tr><td><hr color="#CC0033"/></td></tr>
<tr><td align="center">
<table style= "border-style:solid;border-color:#CC0033;border-width:thin;align:center">
<tr><td><label for="username">Username: </td><td><input name="username" t id="username" type="text" size="25" /></label></td></tr>
<tr><td><label for="password">Password: </td><td><input name="password" id="password" type="password" size="25" /></label></td></tr>
<tr><td><input type="checkbox" name="setcookie" value="setcookie" /> Remember Me</td></tr>
<tr align="center"><td colspan="2"><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset"/></td></tr></table>
</td></tr>
</form>

<!--The follwing row is just for pushing the footer to the bottom-->
<tr height="90"><td></td></tr>
<tr><td><br /><hr color="#CC0033"/></td></tr>
<tr><td align="center"><p class="smallFont">King Saud University. All rights reserved,2008</p></td></tr>
</table>
</body>
</html>


newlogin.php


<?php
/* These are our valid username and passwords */
$user = 'test';
$pass = 'test';

if (isset($_POST['username']) && isset($_POST['password'])) {

if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {

if (isset($_POST['rememberme'])) {
/* Set cookie to last 1 year */
setcookie('username', $username, time()+60*60*24*365, '', '');
setcookie('password', md5($password), time()+60*60*24*365, '', '');

}
else {
/* Cookie expires when browser closes */
setcookie('username', $username, false, '', '');
setcookie('password', md5($password), false, '', '');
}

$body = '<h1>Registered!</h1><p>Thank you <b>'.$username.'</b>, your information has been added to the database, you may now <a href='adminHome.htm' title='Login'>log in</a>.</p>';

}

else {
$body = 'Username/Password Invalid';
}

}

else {
$body = 'You must supply a username and password.';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?php echo $body; ?>

</body>
</html>


Hope this helps.

Smile
05-30-2008, 01:55 PM
Thanks alot for replying

I found problem when change echo to header in
header('Location: newlogin.php');
because it is open newlogin.php before i enter the password and name...

this the output
"You must supply a username and password."

and also code remember me doesn't work:(