Link to home
Start Free TrialLog in
Avatar of epifanio67
epifanio67

asked on

PHP5: setting session variables ok in Firefox 31; but failing on other browsers, any ideas why?

Hello Experts,

I am able to set session variables across pages ok in Firefox 31.
However, when I test the same code in Opera 23 and Chrome 36, it fails.
I get "Notice: Undefined index: "

here is the code:

//***** index.php
ini_set('session.use_only_cookies', 1);
$lifetime = 300;
session_name('testSession');
session_set_cookie_params( time() + $lifetime, '/', 'localhost', false, true );
session_start();

$_SESSION['key'] = 'mykeyvalue';

echo '<br> session id: '. session_id() . '<br>';
echo $_SESSION['key'] . "<br>";
echo "<a href=./page1.php>page1</a>";

//****** page1.php
ini_set('session.use_only_cookies', 1);
$lifetime = 300;
session_name('testSession');
session_set_cookie_params( time() + $lifetime, '/', 'localhost', false, true );
session_start();


echo '<br> session id: '. session_id() . '<br>';
echo $_SESSION['key'] . "<br>";
echo "<a href=./>home</a>";

Open in new window


Can you test this code?
Any ideas why it works in Firefox ok, but failing on the other browsers?

Regards,
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

$_SESSION variables work fine and are Not browser dependent.  However, Chrome will NOT set a cookie on 'localhost'.  Not ever.  Your site must be identified by an IP address or domain name for Chrome to set a cookie.  It is considered a 'security issue' and you can expect that eventually Firefox will do the same.
Avatar of epifanio67
epifanio67

ASKER

Thanks Dave,

Here is my confusion: now, I try the exact same code and works ok on Firefox, Opera, and Chrome... I am able to pass session vars ok...

BUT

I just tried it using my iphone (safari) and ipad (safari) and I get the undefined index....

this is too weird....

Any other thoughts?

(I do see the cookie in Chrome though.... are you sure it is not allowed?)

Thanks for your help....

Regards
You say that you are seeing a cookie for the domain 'localhost' in Chrome?  I don't think so.

There are two important concepts you need to understand.  First, 'localhost' is always and only on the current machine that you are on.  Every machine or device has it's own 'localhost'.  Because of that, you can not ever connect to the 'localhost' on another machine... because 'localhost' always and Only refers to the machine you are on.

Second, cookies are set to be valid only on the domain that sets them.  If your browser is on the same machine that the web server is on, then a page might be able to set a cookie.  If your browser is on another machine, trying to set a cookie for 'localhost' which is Always and Only on your current machine will fail because it can not refer to the domain that the web page is coming from.  Because 'localhost' is referring to the wrong domain and machine.

Though I have seen a lot of demo code that uses 'localhost', I never do that.  For all connections of any kind, I use the IP address or domain name for the web site.  When I do that, all the cookies get set properly because they have a specific domain/IP address.

Here http://en.wikipedia.org/wiki/HTTP_cookie is a lot of info about cookies and what is required.

Another thing that Chrome won't do is load a 'file://' page from an 'http://' page.  That is considered a serious security breach.  https://code.google.com/p/browsersec/wiki/Part2
hi Dave,

Yeah, I am seeing a cookie for domain name 'localhost'.

To be honest, I am surprised that you are surprised.... :-)

When developing, I set up a virtual machine. This is what I call 'localhost'. Then, I network the devices I use to test and troubleshoot. I don't set up a server on each developing or test host.

Regards,
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see now what you are saying... thank you for your patience.. and help...
You're welcome, glad to help.