• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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,
0
epifanio67
Asked:
epifanio67
  • 4
  • 3
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now