Forum Moderators: coopster

Message Too Old, No Replies

IE bad with cookies?

         

ayushchd

10:00 am on Aug 2, 2007 (gmt 0)

10+ Year Member



hi..in my login script im using both cookies ( for username ) and sessions (for password) so as to make it something like orkut...so that when the user closes the site widout logging out, it shud remember the username for sometime and discard the password...for this i hav the following which works perfectly in opera and mozilla but nt in IE. Could anyone suggest me any remedy?

if( (isset($_COOKIE['user'])) && (!session_is_registered("password")) ) {
$message="";
if($Login){ // If clicked on Login button.
$result=mysql_query("select * from users where username='" .$_COOKIE['user']."' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("password");
header("location:main.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
}// End Login authorize check.
if ($message!= ""){
echo "<div id=\"failure\">$message</div>";
}
displayform();

$password and displayform() have already been defined.

Please help.

d40sithui

4:05 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



two things
1) How are you setting your cookie? Did you put an expiration on it? such as
setcookie("user", $user, time()*3600); //expires in 1hr

2) On a side note, your query doesnt seem to be sql injection safe. for security, perhaps you should check inputs before runnign them into your db

see here for more hlep
[us3.php.net...]

ayushchd

4:41 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



setcookie("user", $username, $time+3600);

this is the way i m setting the cookie...here $username =$_POST['username'];
username is the text input field..

I don think that the sql query is creating a problem coz the same code works well in other conditions for eg :

if( (!isset($_COOKIE['user'])) && (session_is_registered("password"))) {
if($Login){ // If clicked on Login button.
// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
setcookie("user", $username, $time+3600);
if ($action == "") {
header("location:main.php");
} else {
header("location:$action.php");
} // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
} // End Login authorize check.
if ($message!= ""){
echo "<div id=\"failure\">$message</div>";
}
displayform ();

this works well in all the browsers

d40sithui

5:27 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



oh, if this is the case,
<?
setcookie("user", $username, $time+3600);

?>

how are you getting your "$time"? or this a typo as you are trying to call the "time()" function as appose to a variable?

also i had a type earlier, it should have been
setcookie("user", $user, time()+3600);

ayushchd

6:36 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



Well, this is my complete login script :

<?
session_start();
if( (isset($_COOKIE['user'])) && (session_is_registered("password")) ){ /// if already signed in then redirect to main.php
header ("location: main.php");
}
include ('mysql.php');
$time = time();
$Login=$_POST['Login'];
$username=$_POST['username'];
$password=md5($_POST['password']); // Encrypt password with md5() function.
function displayform() {?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css.css">
<title>::: VSE Nepal ::: Login</title>
<style type="text/css">
<!--
.style1 {font-size: 80%}
-->
</style>
</head>
<body>
<div id = "forms">
<form id="form1" name="form1" method="post" action="">
<fieldset>
<legend>Login</legend>
<table align="center">
<tr>
<td class="querywhite">User : </td>
<td><div align="left" /> <? if (isset($_COOKIE['user'])) {
if (!session_is_registered("password")) {
echo $_COOKIE['user'];
} } else { echo "<input name=\"username\" type=\"text\" id=\"username\" />";
}?></div></td>
</tr>
<tr>
<td class="querywhite">Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
<tr>
<td><input name="Login" type="submit" id="Login" value="Login" /></td></tr>
</table>
<center><font size="-2">Forgot Username ¦ Forgot Password ¦ Register</center></font>
</fieldset>
</form>
</div>
</body>
</html>
<? }
/////////////////////////////LOGOUT SECTION///////////////////////////////////////////////////
$action = $_GET['action'];
if ($action == "logout") {
$check = "Logged Out";
$time = time();
if (isset($_COOKIE['user']))
{
unset($_SESSION['password']);
setcookie ("user", "", $time - 3600);
}
if($Login){ // If clicked on Login button.
// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
setcookie("user", $username, $time+3600);
session_register("password");
header("location:main.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
}
if ($message!= ""){
echo "<div id=\"failure\">$message</div>";
}
displayform();
} else if( ($action!= "logout") ){
///////////////////////////////////////////////////////////////////////////////////////////
if( (!isset($_COOKIE['user'])) && (!session_is_registered("password")) ) {
$message="";
if($Login){ // If clicked on Login button.
// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.

setcookie("user", $username, $time+3600);
session_register("password");
if ($action == "") {
header("location:main.php");
} else {
header("location:$action.php");
} // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
} // End Login authorize check.
if ($message!= ""){
echo "<div id=\"failure\">$message</div>";
}
displayform ();
} else if( (isset($_COOKIE['user'])) && (!session_is_registered("password")) ) {
$message="";
$check="Cookie Yes. Session No.";

if($Login){ // If clicked on Login button.
$result=mysql_query("select * from users where username='" .$_COOKIE['user']."' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("password");
if ($action == "") {
header("location:main.php");
} else {
header("location:$action.php");
} // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
}// End Login authorize check.
if ($message!= ""){
echo "<div id=\"failure\">$message $check</div>";
}
displayform();
} else if( (!isset($_COOKIE['user'])) && (session_is_registered("password"))) {
if($Login){ // If clicked on Login button.
// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
setcookie("user", $username, $time+3600);
if ($action == "") {
header("location:main.php");
} else {
header("location:$action.php");
} // Re-direct to main.php
exit;
}else{ // If not match.
$message=" Incorrect Username or Password ";
}
} // End Login authorize check.
if ($message!= ""){
echo "<div id=\"failure\">$message</div>";
}
displayform ();
}}?>

Everything works well except for the condition : COOKIE SET, SESSION EXPIRED

dreamcatcher

7:11 am on Aug 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try setting your error reporting level to report all errors. After your opening <?php tag add:

error_reporting(E_ALL);

And see if that throws any errors.

dc

ayushchd

10:23 am on Aug 3, 2007 (gmt 0)

10+ Year Member



Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\virtual\index.php:8) in C:\Program Files\Apache Group\Apache2\htdocs\virtual\index.php on line 58

This is the warning i get

d40sithui

10:50 am on Aug 3, 2007 (gmt 0)

10+ Year Member



this is the exact error when you attempt to set the cookie after displaying html. setcookie needs to come BEFORE any HTML output.

vincevincevince

10:54 am on Aug 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put this as your very very first line:
<?php ob_start();?>

ayushchd

10:55 am on Aug 3, 2007 (gmt 0)

10+ Year Member



no...doing that doesnt work either.

ayushchd

12:57 pm on Aug 3, 2007 (gmt 0)

10+ Year Member



SOLVED!

The problem was that the register_globals was turned on i had to turn it off...its now working fine....but my registration page is now not working fine :( ........Please see if u can help me in that..