Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Cookies not expiring? Can figure out why!!
Old 02-08-2008, 03:45 PM Cookies not expiring? Can figure out why!!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay,

i made a simple shoutbox @ http://dansgalaxy.co.uk/shoutbox/

but i want the cookie to expire and reset its self each time a user posts so if they changed the name it remembers it, but it seems to be stuck with it.

really annouyinh.

okay heres the code:

PHP Code:
<?php
$db_host 
'localhost';
$db_user 'dan_admin';
$db_pass 'PASSWORD';
$db_name 'dan_shoutbox';
mysql_connect($db_host$db_user$db_pass);
mysql_select_db($db_name);
if(isset(
$_POST['shout']))
{
setcookie('shoutbox_user'"",1'/'"http://dansgalaxy.co.uk");
$user mysql_real_escape_string($_POST['user']);
$msg mysql_real_escape_string($_POST['msg']);
$datetime date('Y-m-d H:i:s');
$ip $_SERVER['REMOTE_ADDR'];

mysql_query("INSERT INTO messages SET user='$user',msg='$msg',datetime='$datetime',ip='$ip'") or die(mysql_error());

 
//Expire cookie in year.
 
$expire time()+(60*60*24*1);
 
setcookie('shoutbox_user'"$user","$expire"'/'"http://dansgalaxy.co.uk");
}
 
print_r($_COOKIE);
echo 
'<div style="width: 430px; border: 1px solid #000; height: 150px">';
echo 
'<div style="width: 150px; float: left;">
<form action="" method="post">
<label for="user">User:</label><br />
<input type="text" name="user" value="'

if(!empty(
$_COOKIE['shoutbox_user'])) 

echo 
'c:'.$_COOKIE['shoutbox_user']; 

else{ 
/*echo'Guest';*/ echo $_COOKIE['shoutbox_user']; }
echo 
'" size="15" />
<br />
<br />
<label for="msg">Message:</label><br />
<input type="text" name="msg" value="" size="15" />
<br />
<br />
<input type="submit" name="shout" value="Shout!" />
</form>
</div>'
;
echo 
'<div style="overflow-y:auto; width: 275px; border: 1px solid #000; height: 140px">';
if (!isset(
$_GET['pagenum']))

$pagenum 1

else { 
$pagenum $_GET['pagenum'];
}
$result mysql_query("SELECT * FROM messages ORDER BY datetime DESC") or die("ERROR:".mysql_error());

$rows mysql_num_rows($result); 
//This is the number of results displayed per page 
$page_rows 8
//This tells us the page number of our last page 
$last ceil($rows/$page_rows); 
//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum 1

$pagenum 1

elseif (
$pagenum $last

$pagenum $last

//This sets the range to display in our query 
$max 'ORDER BY datetime DESC LIMIT ' .($pagenum 1) * $page_rows .',' .$page_rows
//This is your query again, the same one... the only difference is we add $max into it
$messages_res mysql_query("SELECT * FROM messages $max") or die("ERROR:".mysql_error());
while(
$msg mysql_fetch_array($messages_res))
{
echo 
'<strong>'.$msg['user'].':</strong> ';
echo 
''.$msg['msg'].'<br /><hr />';
}
echo 
'</div>';
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum != && $pagenum != 0
{
/*
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'>&lt;-First</a> ";
echo " ";*/
$previous $pagenum-1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> &lt;-Previous</a> ";

for(
$i 1$i <= $last$i++)
{
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$i'> $i</a> ";
}
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum != $last
{
$next $pagenum+1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -&gt;</a> ";
#echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}  
echo 
'</div>';
?>
i cant see why :s from what i can tell once they make a post, it empties and expires any old Cookie, inserts and then resets it with what ever user they enterd on that post.

Then it should show it in the user field...

but its not
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 02-08-2008, 04:10 PM Re: Cookies not expiring? Can figure out why!!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
° When there is a post, you set 2 times the same cookie. Why ?
° The 5th parameter of setcookie() is the domain, so put a domain in it, not an url.
Quote:
The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain
Remember that you cannot access the content of a cookie you setted in the same page load.
You need to reload the page to read the content of the cookie.
Here, after having set the value of the cookie, you access $_COOKIE[], which will hold the old value, and not the new one.

Could be your problem.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-08-2008, 05:21 PM Re: Cookies not expiring? Can figure out why!!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
The first set cookie is "supposed" to clear any old cookie which has been previously set.

I will through a refresh/redirect in at the end of the if(post) see if it helps but even after many many mnay refreshes it still dont work.

PHP Code:
<?php
$db_host 
'localhost';
$db_user 'dan_admin';
$db_pass 'PASS';
$db_name 'dan_shoutbox';
mysql_connect($db_host$db_user$db_pass);
mysql_select_db($db_name);
if(isset(
$_POST['shout']))
{
setcookie('shoutbox_user'"",1'/'".dansgalaxy.co.uk");
$user mysql_real_escape_string($_POST['user']);
$msg mysql_real_escape_string($_POST['msg']);
$datetime date('Y-m-d H:i:s');
$ip $_SERVER['REMOTE_ADDR'];

mysql_query("INSERT INTO messages SET user='$user',msg='$msg',datetime='$datetime',ip='$ip'") or die(mysql_error());

 
//Expire cookie in year.
 
$expire time()+(60*60*24*1);
 
setcookie('shoutbox_user'"$user","$expire"'/'".dansgalaxy.co.uk");
 
header('Location: http://dansgalaxy.co.uk/shoutbox/');
}
 
print_r($_COOKIE);
echo 
'<div style="width: 430px; border: 1px solid #000; height: 150px">';
echo 
'<div style="width: 150px; float: left;">
<form action="" method="post">
<label for="user">User:</label><br />
<input type="text" name="user" value="'

if(!empty(
$_COOKIE['shoutbox_user'])) 

echo 
'c:'.$_COOKIE['shoutbox_user']; 

else{ 
/*echo'Guest';*/ echo $_COOKIE['shoutbox_user']; }
echo 
'" size="15" />
<br />
<br />
<label for="msg">Message:</label><br />
<input type="text" name="msg" value="" size="15" />
<br />
<br />
<input type="submit" name="shout" value="Shout!" />
</form>
</div>'
;
echo 
'<div style="overflow-y:auto; width: 275px; border: 1px solid #000; height: 140px">';
if (!isset(
$_GET['pagenum']))

$pagenum 1

else { 
$pagenum $_GET['pagenum'];
}
$result mysql_query("SELECT * FROM messages ORDER BY datetime DESC") or die("ERROR:".mysql_error());

$rows mysql_num_rows($result); 
//This is the number of results displayed per page 
$page_rows 8
//This tells us the page number of our last page 
$last ceil($rows/$page_rows); 
//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum 1

$pagenum 1

elseif (
$pagenum $last

$pagenum $last

//This sets the range to display in our query 
$max 'ORDER BY datetime DESC LIMIT ' .($pagenum 1) * $page_rows .',' .$page_rows
//This is your query again, the same one... the only difference is we add $max into it
$messages_res mysql_query("SELECT * FROM messages $max") or die("ERROR:".mysql_error());
while(
$msg mysql_fetch_array($messages_res))
{
echo 
'<strong>'.$msg['user'].':</strong> ';
echo 
''.$msg['msg'].'<br /><hr />';
}
echo 
'</div>';
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum != && $pagenum != 0
{
/*
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'>&lt;-First</a> ";
echo " ";*/
$previous $pagenum-1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> &lt;-Previous</a> ";

for(
$i 1$i <= $last$i++)
{
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$i'> $i</a> ";
}
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum != $last
{
$next $pagenum+1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -&gt;</a> ";
#echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}  
echo 
'</div>';
?>
Okay using this now, and added a refresh in there

and now it dont even seem to be setting ANY cookies as im getting nothing back in my cookie array.
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 02-08-2008 at 05:25 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 02-08-2008, 06:04 PM Re: Cookies not expiring? Can figure out why!!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay not sure whats changed but i been doing a couple of bits and it seems to have strated working how i want :s
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Cookies not expiring? Can figure out why!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49111 seconds with 12 queries