Jump to content

setcookie() doing it's job


clown[NOR]

Recommended Posts

Hi!

 

This is my first time here on your forum, and it looks like a good PHP community. I'm gonna go straight to the case... I've made a shoutbox, and now i'm trying to add cookies to it so the suer don't have to write their name and www address everytime they want to leave a shout.. but the setcookie() wont work.

I've used the following to double check the cookie

<?php
if (isset($_COOKIE['sbname'])) { echo $_COOKIE['sbname']; }
else { echo "Could not find any cookies matching that name!"; }
?>
<br />
<?php
if (isset($_COOKIE['sburl'])) { echo $_COOKIE['sburl']; }
else { echo "Could not find any cookies matching that name!"; }
?>

 

but all it returns is "Could not find....."

 

The following is the exact code I use to add the shouts: (only change is the filename)

<?php

setcookie("sbname", $_REQUEST['sbname'], time()+31536000);
setcookie("sburl", $_REQUEST['sburl'], time()+31536000);

$phps_sbname = $_REQUEST['sbname'];
$phps_sburl = $_REQUEST['sburl'];
$phps_sbmessage = $_REQUEST['sbmsg'];

if ($phps_sburl == '') {
	$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong>" . $phps_sbname . ":</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
}
elseif ($phps_sburl != '') {
	$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong><a href=" . $phps_sburl . " target=_blank>" . $phps_sbname . "</a>:</strong> 	" . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
}


#echo "Name: " . $phps_sbname . "<br>URL: " . $phps_sburl . "<br>Message: " . $phps_sbmessage . "";
$phps_sbfh = fopen("filename", "ab");
fwrite($phps_sbfh,$phps_newshout);
fclose($phps_sbfh);
Header("Location: http://www.nstclan.com/phpschool/v1.1/index.php");


?>

 

Anyone having any ideas that might help me out here?

 

Regards,

Clown[NOR]

Link to comment
https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/
Share on other sites

www.php.net/setcookie

 

I would look into the full parameters of set cookie. But either way your code is flawed. You need the setcookie to be inside of an if statement: IE:

<?php
// if we have a value set it.
if (isset($_REQUEST['sbname'])) {
       if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) {
   // unset previous cookie
          setcookie("sbname", '', time()-31536000);
   setcookie("sburl", '', time()-31536000);
       }

     // set new cookie
        setcookie("sbname", $_REQUEST['sbname'], time()+31536000);
       setcookie("sburl", $_REQUEST['sburl'], time()+31536000);
}
?>

 

What was happening was you were setting the cookie with an empty value and therefore it was not "set".

I've tried many different ways now, but still no luck.. just can't figure out wth I'm doing wrong.. And it's probably not that werid that I cant see it... I've only been working with more advanced php than include() for about 3 days now...  ;D But my code looks like this atm:

 

<?php
$phps_sbname = $_REQUEST['sbname'];
$phps_sburl = $_REQUEST['sburl'];
$phps_sbmessage = $_REQUEST['sbmsg'];

if ($phps_sbname == '') { echo 'Please enter a name'; }
elseif ($phps_sbmessage == '') { echo 'Please enter a message'; }
else {		
	if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) {
		setcookie("sbname", $_REQUEST['sbname'], time()-31536000);
		setcookie("sburl", $_REQUEST['sburl'], time()-31536000);
	}
	setcookie("sbname", $_REQUEST['sbname'], time()+31536000);
	setcookie("sburl", $_REQUEST['sburl'], time()+31536000);
	if ($phps_sburl == '') {
		$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong>" . $phps_sbname . ":</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
	}
	elseif ($phps_sburl != '') {
		$phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong><a href=" . $phps_sburl . " target=_blank>" . $phps_sbname . "</a>:</strong> 	" . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>";
	}

	$phps_sbfh = fopen("filename.txt", "ab");
	fwrite($phps_sbfh,$phps_newshout);
	fclose($phps_sbfh);
	Header("Location: http://www.nstclan.com/phpschool/v1.1/index.php");
}
?>

You are still always reseting the cookie, you only want to set the cookie if it is necessary.

 

Think through that logic, how can I only set the cookie if it is necessary??? Than you will have the answer to your question.

 

<?php
// if we have a value set it.
if (isset($_REQUEST['sbname'])) {
       if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) {
   // unset previous cookie
          setcookie("sbname", '', time()-31536000);
   setcookie("sburl", '', time()-31536000);
          unset($_COOKIE['sbname']);
          unset($_COOKIE['sburl']);
       }

     if (!isset($_COOKIE['sbname']))
     // set new cookie
        setcookie("sbname", $_REQUEST['sbname'], time()+31536000);
       setcookie("sburl", $_REQUEST['sburl'], time()+31536000);
    }
}
?>

 

Maybe that logic will help you.

still the same... I just dont understand why it's so hard creating these cookies.. I've used cookies for login and for a couple of tutorials I've made (pageload counter and last visit)... and havent had any problems at all... but now it just won't even create the cookies...

well... heh.. that would be a negative... i dont know how to check that =) but I have no reason to belive that it dont get processed since it's added before the code that writes the info to the shout file... and the shouts appears on the site as they should be... and in my logical thinking that means that it has to process the cookie section before it can continue on to the "add shout" part of the code..

<?php
// if we have a value set it.
if (isset($_REQUEST['sbname'])) {
       if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) {
          // unset previous cookie
//          setcookie("sbname", '', time()-31536000);
//   setcookie("sburl", '', time()-31536000);
          //unset($_COOKIE['sbname']);
         // unset($_COOKIE['sburl']);
           print "cookie would be unset";
       }

     if (!isset($_COOKIE['sbname']))
     // set new cookie
//        setcookie("sbname", $_REQUEST['sbname'], time()+31536000);
  //     setcookie("sburl", $_REQUEST['sburl'], time()+31536000);
         print "new cookie would have been set";
    }
}
?>

 

 

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.