PHP setcookie problem...

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
I have a problem within one of my programs where I cannot set cookies at all.

Code:
[...]
if (conditions)
{
$cookiedata = '123';
setcookie("cookie1", "$cookiedata", time()+604800, '/');
echo "$cookiedata";

}
[...]
I have used the above code, and the page will show 123, as expected. However, there is no cookie!

At first I thought maybe I'm not allowed to set cookies ;)
so I created a new page that looked like the following:
Code:
<?php
$cookiedata = '123';
setcookie("cookie1", "$cookiedata", time()+604800, '/');
echo "$cookiedata";
?>
Again, it outputs the expected 123, but this time it does set a cookie.
Am I missing something here?

BTW, no header information was sent prior to the cookie, and PHP gives no error.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
can you please post all of your code for the first script?

the rest of it might have an answer to your question
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
It concerns login to my site and deals with passwords and such. I have commented out anything I don't want publically known that is also irrelevant. As for the actual setcookie part, I uploaded the '123' test, with identical results as to the normal data it has. Namely, what I previously mentioned.
Code:
<?php
function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) {
  $got=array();
  mysql_data_seek($result, 0);
  while ($row = mysql_fetch_array($result, $numass)) {
    array_push($got, $row);
  }
  return $got;
}

require_once("scripts/mysqllogin.php");

if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = 'index';

if (!file_exists("pages/$page.php"))
$page = 'index';

if (isset($_GET['action']))
$action = $_GET['action'];

require_once("scripts/login.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

[...]
contents of scripts/mysqllogin.php
Code:
<?php
$database  =  array(
'url'      => 'localhost'     ,
'name'     => '*****'       ,
'pass'     => '*****'      ,
'database' => 'data'    ,
'table'    => 'as_nations');

mysql_connect($database['url'],$database['name'],$database['pass']) or die('Error connecting to MySQL: '.mysql_error());
mysql_select_db($database['database']) or die('Error connecting to MySQL database: '.mysql_error());
?>
contents of scripts/login.php
Code:
<?php
$name = '';
$pass = '';
if ($action == 'login')
{
if (isset($_POST['name']))
$name = $_POST['name'];
$name = strtolower(str_replace(' ','_',$name));

if (isset($_POST['password']))
$pass = $_POST['password'];
}
else
if (isset($_COOKIE['as_data']))
{
$cookie = $_COOKIE['as_data'];
$cookie = // ;)
$name   = // ;)
$pass   = // ;)
}

$database['table'] = 'as_provinces';

$query = "SELECT id,name,pass,nation,staff FROM {$database['table']} WHERE name='$name'";
$result = mysql_query($query) or die('Error retrieving data: '.mysql_error());  

$rows = mysql_fetch_array($result);
$data = array();
$data['id']     = $rows['id'];
$data['name']   = $rows['name'];
$data['pass']   = $rows['pass'];
$data['nation'] = $rows['nation'];
$data['staff']  = $rows['staff'];

$pass = crypt($pass, $data['pass']);

if (($action == 'logout') || ($data['pass'] == ''))
$pass = 'log me out! ;)';

if ($pass == $data['pass'])
{
$cookiedata = '123';
setcookie("cookie1", "$cookiedata", time()+604800, '/');
echo "$cookiedata";

$loggedin = array('id' => $data['id'], 'name' => $data['name'],'ucname' => ucwords(str_replace('_',' ',$data['name'])), 'nation' => $data['nation'], 'staff' => $data['staff']);
}
else
{
setcookie("cookie1", "0", 1218000000, '/');
$loggedin = array('id' => '', 'name' => '','ucname' => '', 'nation' => '', 'staff' => 0);
}

?>
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
unless everything else in that script works....

i'm fairly certain that php overwrites the previous variable if it is the same.

so, i might actually put the data into an array. and try that.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
unless everything else in that script works....

i'm fairly certain that php overwrites the previous variable if it is the same.

so, i might actually put the data into an array. and try that.
Yeah, it does. And it's something I take advantage of. For example:
Code:
if (($action == 'logout') || ($data['pass'] == ''))
$pass = 'log me out! ;)';

if ($pass == $data['pass'])
But even if it isn't overwriting a variable, it should still create the cookie, yes?
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
it should...but i'm not a PHP expert persay. lol.

i know the basics...and some of the intermediate/advanced. xD

wait for someone else? lol. dont know what to tell you. It should work.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
It should... Which is why I'm so confused now, lol!
Edit:
Anyone able to help?
 
Last edited:

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Yay! I've finally reached a solution.
or at least a workaround...

I've moved all my set cookies right to the top of my header file, thus they are now set before it connects to MySQL. I guess MySQL was causing the cookies not to set, without giving an error?
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
A mySQL connection should not affect cookies, I deal with login scripts a lot. It could possibly be from the variable $cookiedata is enclosed in "quotes". I remember having funky problems like you are having above.

PS. If you're using Firefox, download Web Developer toolbar if you haven't already. In there, you can view all cookies for a website (and just about every else you can dream of, and some you cant), which would be very useful in this case.

-xP
 
Top