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,
epifanio67Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
$_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.
0
epifanio67Author Commented:
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
0
Dave BaldwinFixer of ProblemsCommented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

epifanio67Author Commented:
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,
0
Dave BaldwinFixer of ProblemsCommented:
I never use 'localhost' or virtual machines.  You normally can't use 'localhost' on 'real' websites, why use it for testing?  And 'localhost' is a specific network connection.  The article above tells all about it.

Do you understand why a page coming from one machine can not set a cookie for 'localhost' on a remote machine?  'localhost' is always and only on the 'local' machine therefore a remote machine should not be able to set a cookie for 'localhost' that is on another machine.  Security barriers alone should prevent that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
epifanio67Author Commented:
I see now what you are saying... thank you for your patience.. and help...
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.