Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Set cookie problem

Links for php scripts

Moderators: macek, egami, gesf

Set cookie problem

Postby KasunL » Mon Apr 16, 2012 5:37 pm

Hi!
i tried to set a cookie on the remote server:

Code: Select all
setcookie("user", $cuser, time()+3600);


and i got this error:
Code: Select all
Warning: Cannot modify header information - headers already sent by (output started at /www/zzl.org/s/l/p/slprogrammersheaven/htdocs/test1/logincode.php:2) in /www/zzl.org/s/l/p/slprogrammersheaven/htdocs/test1/logincode.php on line 34


What may causing this?
Thanks for any help.
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Set cookie problem

Postby minimihi » Mon Apr 16, 2012 9:45 pm

You have to set cookies at the very beginning of you HTML.

You set cookies at 34th line which might not be beginning of HTML, it would be different if your buffer your HTML
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Set cookie problem

Postby KasunL » Mon Apr 16, 2012 10:22 pm

>You have to set cookies at the very beginning of you HTML.
The php code is above the <HTML> tag. Is that what you are referring to?
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Set cookie problem

Postby minimihi » Tue Apr 17, 2012 12:56 am

Yes, that's right

should look something like that
Code: Select all
<?php
setcookie
("user"$cusertime()+3600);
?>
<html>...</html>

or in this principle:
Code: Select all
<?php
$output 
"<html>...</html>";
setcookie("user"$cusertime()+3600);
echo 
$output;
?>
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Set cookie problem

Postby KasunL » Tue Apr 17, 2012 8:23 am

But it's already above the html code. php code is right at the beginning even it if has a 30 something lines ? :help:
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Set cookie problem

Postby minimihi » Tue Apr 17, 2012 8:54 am

Then you must have whitespace. Check your code at 2nd line.

Whitespace could look like that:
Code: Select all
 <?php
...php_code...
?>

See empty space before '<?php'? That's called whitespace. Also could be empty line.
Everything what's not inside of PHP code declaration, is considered to be part of HTML. And when headers come in to game, it could mess them up.

oh it's written on function description :-x
setcookie()
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Set cookie problem

Postby KasunL » Tue Apr 17, 2012 10:18 am

Removed all whitespaces, but still get that error (line 30). The full code:
Code: Select all
<?php
DEFINE ('DB_USER', '########');
DEFINE ('DB_PASSWORD', '########');
DEFINE ('DB_HOST', 'slprogrammersheaven.zzl.org');
DEFINE ('DB_NAME', 'slprogrammersheaven_zzl_comments');
$username = $_POST['username'];
$email = $_POST['email'];
$password1 = $_POST['password1'];
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
@mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
$result = mysql_query("SELECT username, email, password1 FROM users") OR die (mysql_error());
$i=0;
$found=false;
$cuser;
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
   $i1 = $i+1;
   $i2 = $i+2;
//   echo $row[$i].'<br>'.$row[$i1].'<br>'.$row[$i2];
   if($row[$i]==$username && $row[$i1]==$email && $row[$i2]==$password1){
//      echo $row[$i].'<br>'.$row[$i1].'<br>'.$row[$i2];
      $found=true;
      $cuser = $row[$i];
   }
}
if($found==true){
   $_SESSION['curntuser']=$cuser;
   echo 'Welcome '.$cuser.'!';
   setcookie("user", $cuser, time()+3600); //ERROR
   echo '</br></br>';
   echo '<a href="index.php">Click here</a>';
}
if($found==false){
   echo 'We cant find you. Please try again.';
   echo '</br></br>';
   echo '<a href="login.php">Login</a>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Set cookie problem

Postby minimihi » Tue Apr 17, 2012 11:29 am

That's echo statemen at 30th line :? Dunno... try to comment out those echo statements?
Your code actually works just fine for me.

Does stand alone setcookie() in PHP file sets your cookies?
Code: Select all
<?php
setcookie
("user", "hah", time()+60);
?>
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Set cookie problem

Postby KasunL » Tue Apr 17, 2012 5:38 pm

ah i just deleted the two lines above setcookie() and the error did not happen. There's something wrong with these two lines:
Code: Select all
$_SESSION['curntuser']=$cuser;
echo 'Welcome '.$cuser.'!';

? :?
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Set cookie problem

Postby KasunL » Tue Apr 17, 2012 7:16 pm

ok i replaced those 2 lines below setcookie() and it works now. Thanks minimihi for the help, appreciate it.
KasunL
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am


Return to PHP Scripts

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.