I believe there may be something wrong with my PHP.ini as I can create cookies using the setcookie function but cannot access the cookie using the COOKIE function.

Please note: I have enabled cookies in my browser

I then have 2 very simple scripts in separate files

file 1

session_start();
$username="thorby68@live.co.uk";
setcookie('sb_cookie', $username,  time()+10000;

file 2

session_start();
echo $_COOKIE['sb_cookie'];

I can see the cookie is created in my local folder, however I am getting an error
Notice: Undefined index: sb_cookie in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sandbbookingsystem\test\test.php on line 4

I have attached a copy of the created cookie, and the session section of my php.ini and would very much appreciate any help.

Recommended Answers

All 8 Replies

on file one there is a missing ")" on this line

setcookie('sb_cookie', $username,  time()+10000;

it should be like this

setcookie('sb_cookie', $username,  time()+10000);

Apologies,

bad typing on my part.

I have the correct syntax in my script, and the cookie gets written to my local default folder. However when I want to access the cookie from the server it is not being read. Is there a path that should be set in the php.ini ?

Well there are a few factors that could cause this problem. First correct your code as ApocDen mentioned. Then when viewing the pages, make sure it is in the same browser window (eg. different tabs) and be sure that cookies are enabled. If cookies are disabled then the script will not work. Also cookies do not transfer between different browsers. So if for example you opened file1 in Internet Explorer and file 2 in Firefox there is no way for the data to transfer since they are different browsers.

Many thanks for your help.

I'm using IE 8 for this purpose and I've enabled cookies (in fact I have set IE to accept all cookies and override default settings). All code opens up with IE 8 and in a single same browser tab.

Further, I have tried the code with Google Chrome and Firefox. Eeach time I use a different browser the browser is used in isolation of the others and everything is executed in the same tab.

I can also see the session being created, and the cookie being created.....it's very strange. Attached is the session settings of php.ini and my cookie. Your help would be very much appreciated.

Perhaps try renaming the cookie so that it only contains letters and numbers. I have created a debugger script so try the following:

file 1

session_start();
$username="thorby68@live.co.uk";
setcookie('sbcookie', $username,  time()+10000);

file 2

session_start();
if (isset($_COOKIE['sbcookie'])) {
if (!empty($_COOKIE['sbcookie'])) {
echo $_COOKIE['sbcookie'];
} else {
echo '<i>This cookie has been emptied or not transfered properly</i>';
}
} else {
echo '<i>This cookie does not exist as far as php can see</i>';
}

once again,

many thanks for your help.

Something very strange is happening here. If I now run file 1 and file 2 in the same folder, the whole process of creating the cookie and reading the cookie works fine and dandy.

However, if I move file 2 from folder root/test to root/pages/admin, the cookie is still created on my local system but I get the error "This cookie does not exist as far as php can see".

Also, I have run a repair on both Apache and PHP to ensure there is not an application/installation problem there.

Was reading the documentation and found your bug. Try the following as I have fully corrected your original problem.

session_start();
$username="thorby68@live.co.uk";
setcookie('sb_cookie', $username,  time()+10000, '/');

Thats excellent. Many thanks.

Also, reading the manual this makes absolute sense. I believe my problem is that I had performed a similar task in another domain and did not include the path at all (as in my original post).

I will be sure to include the path and or subfolders in future.

Many thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.