Jump to content

Help with setcookie()


MesaFloyd

Recommended Posts

Merry Xmas to those on this foruum

Older guy here with some experience but not allot so please be gentle.

I am having problems with setting cookies on my web site.  At one time they worked and for the life of me I cannot find the problem... 3 days now ... no joy. 

To make it as simple as I can, I wrote a very simple program that should demonstrate the problem... I am not that good on server settings, but i have a hunch it is on the server.  My program (testcookie.php)  has phpinfo() combined with the small program that demonstrates so you can see the exact problem.

Can anyone see what I am doing wrong?

BTW, I have no .htaccess file.

You can see the problem here .. almcom<dot>net/testcookie.php

Here is the same code you will see on the above location.

<?php
setcookie("IM_A_COOKIE", "IM_COOKIE_CONTENT", time() + 3600, "/");  //cookie expires in 60min

echo "<br />Running testcookie.php ------- SEE BELOW phpinfo() -------";

echo "<br />-----------------------------------begin display of testcookie.php code------------------<br />";
highlight_file("testcookie.php"); 
echo "<br />----------------------------------------end of testcookie.php code------------------------<br /><br />";

if (isset($_COOKIE["IM_A_COOKIE"]))
  echo "It was set... " . $_COOKIE["IM_A_COOKIE"] . "!<br />";
else
  echo "<h4>Cookie was <font color=red>NOT</font> set</h4>";

echo "-----------------------------------------------------------------------------------";
echo "<br />Here is the phpinfo():...";
phpinfo();
?>
<HTML>
<HEAD><TITLE><br /><br />Cookie Tester</TITLE></HEAD>
<BODY>END of testing with testcookie.php</BODY>
</HTML>

Link to comment
Share on other sites

Once you set a cookie it will not be available via the $_COOKIE until the next page reload. Here is a simple test on cookie setting:

 

<?php
if (isset($_GET['set_cookie'])) {
    setcookie("name", "value", time()+3600, "/");
}elseif (isset($_GET['unset_cookie'])) {
    setcookie("name", false, time()-3600, "/");
    unset($_COOKIE['name']); 
}

if (isset($_COOKIE['name'])) {
    $output .= "The cookie name with a value of " . $_COOKIE['name'] . "<br /><a href=testcookie.php>Click here to refresh the page</a> <a href=testcookie.php?unset_cookie=yes>Click here to unset it</a>";
}elseif (isset($_GET['set_cookie'])) {
    header("Location: testcookie.php");
}else {
    $output .= "The cookie has not been set yet. <a href=testcookie.php?set_cookie=yes>Click Here</a> to set one.";
}

echo $output;
?>

 

That is a simple way to test cookie setting and un-setting. The code may have some syntax errors etc as it is untested, but hopefully you get the idea. (Note that the site does a redirect after the setting of the cookie so you can see the cookie, as without that you will not be able to access the cookie till the next page reload).

Link to comment
Share on other sites

thanks premiso, I will take a look at what you suggest...

but...

when I run my script (yes I understand, it will not show the cookie in the same page) it does not put a cookie on my two pc's... (I am looking after every visit...)

 

I will load your suggestion and keep beating on the, thanks

Floyd

Link to comment
Share on other sites

it does not put a cookie on my two pc's...

You are likely getting a header error that prevents the cookie from being sent.

 

Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you by displaying all the errors it detects?

 

Use a phpinfo() statement to confirm that the two settings actually get changed after you restart the web server to get any change made to php.ini to take effect.

Link to comment
Share on other sites

Hi PFM... thanks for your help

 

You will notice, I already have the phpinfo() on this page also to help find the problem if it is on the server settings.

 

I have added this to the testsetcookie.php

---

ini_set ("display_errors", "1");

error_reporting(E_ALL);

---

This testcookie.php web page is on a professional hosting site so I cannot immediatly reset the servers

 

If you go here, you can see it all

http://almcom.net/testcookie.php

 

Hope you can spot something,,, I just don't see it...

presimo, I am working on your suggestion also.

 

Thanks to you both again for helping, I will continue my beating.

Floyd

Link to comment
Share on other sites

output started at ..../testcookie.php:1 (line 1)

 

If there are no actual characters in the file before the <?php tag, then your file has been saved as a UTF-8 encoded file and the BOM (Byte Order Mark) characters that the editor placed at the start of the file is the output that is being sent that is preventing the header/cookie from working.

 

Either save your file as an ANSI (ASCII) encoded file or if you must save it as a UTF-8 encoded file, save it without the BOM.

Link to comment
Share on other sites

premiso

I have taken your code and put it here with the errors to be viewed..

I am not great at programmaing but it shows error and I don't see where is is from

You can see function here with the code also...

almcom.net/testcookie2.php

 

Here is the code only

<?php

ini_set ("display_errors", "1");
error_reporting(E_ALL);

if (isset($_GET['set_cookie'])) {
    setcookie("name", "value", time()+3600, "/");
}elseif (isset($_GET['unset_cookie'])) {
    setcookie("name", false, time()-3600, "/");
        unset($_COOKIE['name']);
}
if (isset($_COOKIE['name'])) {
    $output .= "The cookie name with a value of " . $_COOKIE['name'] . "<br />
    <a href=testcookie2.php>Click here to refresh the page</a>
    <a href=testcookie2.php?unset_cookie=yes>Click here to unset it</a>";
}elseif (isset($_GET['set_cookie'])) {
    header("Location: testcookie2.php");
}else {
    $output .= "The cookie has not been set yet. 
    <a href=testcookie2.php?set_cookie=yes>Click Here</a> to set one.";
}
echo $output;

echo "<br /><br /><br />------begin display of testcookie2.php code----<br />";
highlight_file("testcookie2.php"); 
echo "<br />------end of testcookie2.php code---------------<br /><br />";

?>

 

Floyd

Link to comment
Share on other sites

PFM

Clearly, I'm out of your league  ;)...

I am using Sharepoint Designer... What you see is the exact characters put on the web site.... noting before nor after.. (I know I know... a wysiwyg arrrrrg!!!) but it is PHP, so it is only the pure code I am working on.

Hope I am making sense...

Floyd

 

 

Link to comment
Share on other sites

Actually, the code you are testing with from premiso produces the same error on line 1 when you set the cookie().

 

Your editor must have some method within the 'file save as' menu to save as an ANSI encode file or to save a UTF-8 encoded file without the BOM.

 

If you cannot find a method to save the file so that headers will work, I would recommend using Notepad++ http://notepad-plus.sourceforge.net/uk/site.htm , even if just to save the file the way you want (and need) it to be.

Link to comment
Share on other sites

PFM...

WOW!, that did it... now to take that to the actual page I am(was) having problems with.It will take a while to get to it but thank you sooooo much....

I "ASSUMED" the file and what i saw was pure ASCII, but apparently it is embedding something before the first line... and saving it as ASCII removed it..

I save it in notepage(ASCII) and then uploaded it as I usually would.  Ive been on this for 3 days, and NEVER would have gotten that.

 

I cant thank you enough.

(I hope this is transferrable to my web page I've been pulling (whats left of) my hair.)

we will see...

 

Have a great remainder of Xmas, and New Year

Floyd

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.