Jump to content

[SOLVED] setcookie and $_COOKIE


jmr3460

Recommended Posts

I am not sure what is happening here. If I change the name of the $_COOKIE['setcount']  then counter counts every refresh When I say !isset then white screen. Can someone help.

 

<?php
setcookie("setcount","baseball",time()+7200,"/");

$host = "localhost";
$user = "user";
$pass = "pass";
$database = "counter";
$ip = getenv('REMOTE_ADDR');
$date = date("M-d-Y h:i:s", time()+3600);
mysql_connect($host, $user, $pass);
mysql_select_db($database) or die(mysql_error());

if (isset($_COOKIE['setcount']))
{
$data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo $info['id'];
}
else
	{
mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error());
$data = mysql_query("SELECT id FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo $info['id'];
}
?>

My hope here is to have a counter that only counts if the cookie is not set. Right now it counts every refresh.

Link to comment
https://forums.phpfreaks.com/topic/161259-solved-setcookie-and-_cookie/
Share on other sites

OK I have moved some things around I had a major flaw in the code up above. I set my cookie then I ask if cookie is set to do something. Now I have moved some things in front of my setcookie function and if cookie in not set then it will setcookie then give me the id to the last row on the DB. Although when I refresh the id number goes blank. Here is the new code:

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "counter";
$ip = getenv('REMOTE_ADDR');
$date = date("M-d-Y h:i:s", time()+3600);
mysql_connect($host, $user, $pass);
mysql_select_db($database) or die(mysql_error());
if (!isset($_COOKIE['april']))
{
setcookie("april","baseball",time()+7200,"/");
mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error());
$data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo $info['id'];
}
else
	{
$data = mysql_query("SELECT * FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo $info['id'];
}
?>

If I change the name of the cookie then it will count again.

I have tried every way I can think of and I get the same results. I feel that I am almost there yet it is only the first step to the site I want to build. The only way I get this counter to work is to change the cookie name. I have tried January through December and I think I am on Wednesday now. I need to go to bed. The sun is coming up. This is the code I have gotten to so far. I know the first part works it is the part that sets the cookie. and if the cookie is set then the counter does not increment. so I fel that the first part is good. I think that the part from else on down is the problem.  I will go through this. I open the DB then check for cookie. If the cookie is not set then I set the cookie and insert a row in the DB. It sets ip address and time in Central time zone for me. Then I select all or id from table where the last thing I set becomes the focus (Thanks Ken2k7). Then I pull and print the id that I am focused on to print on the page. Else I echo the id I am focused on. It is the last part that is not happening. Here is the code I have once again:

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "counter";
$ip = getenv('REMOTE_ADDR');
$date = date("M-d-Y h:i:s", time()+3600);
mysql_connect($host, $user, $pass);
mysql_select_db($database) or die(mysql_error());
if (!isset($_COOKIE['wednesday']))
{
setcookie("wednesday","baseball",time()+7200,"/");
mysql_query("INSERT INTO comments values('','$date','$ip','','')") or die(mysql_error());
$data = mysql_query("SELECT id FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo $info['id'];
}
else
	{
$data = mysql_query("SELECT id FROM comments WHERE id = LAST_INSERT_ID()") or die(mysql_error());
$info = mysql_fetch_array($data);
echo  $inf0['id'];
}
?>

 

Thanks for any advise you can give.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.