I am trying to just set a session variable on one page, then load it on another page. This code works on Firefox on Windows 7. It does not work when accessing the same pages on my iPod Touch.
The first page is like this:
session_start();
$id = "e0vgrejdmkjbltrdrtqtnjgzmy1cqurfluuzodeyqjlcoey5rx0";
$_SESSION['id'] = $id;
The second page contains this code:
session_start();
$id = $_SESSION['id'];
echo "ID is $id";
The output then shows as:
ID is
If I run this code:
echo "Cookie ";
print_r($_COOKIE);
echo "Session ";
print_r($_SESSION);
The output on my Win开发者_开发技巧dows machine is:
Cookies Array ( [__utma] => 118825363.834893446.1282611798.1283521628.1283525684.13 [__utmz] => 118825363.1283397018.9.7.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=spf test [PHPSESSID] => 6333ddae9f0514f7c4744e340a6746d1 [__utmc] => 118825363 [__utmb] => 118825363.5.10.1283525684 [id] => eza3odazrtcxlty5mdutoem2ms05ndqxltrcnzvgrjjbnzrgqn0 ) Session Array ( [id] => ezffmddgmdmxluvcmzmtruvfnc0wmdu4ltrfnzddnevfmdhfnx0 )
and the output on my iPhone is:
Cookies Array ( [PHPSESSID] => 8ec43ead5611dde399d763166926021d [__utma] => 118825363.1927811493.1283218120.1283521676.1283525856.6 [__utmb] => 118825363.1.10.1283525856 [__utmc] => 118825363 [__utmz] => 118825363.1283490501.4.3.utmcsr=(mydomain.com)|utmccn=(referral)|utmcmd=referral|utmcct=/ ) Session Array ( [id] => )
No error happens, it just appears to set $id to be "" I think. This seems simple enough. Is there something I am missing where you cannot do this on an Apple device?
Solved: I was using an absolute URL instead of a relative one. I guess that messes everything up.
The difference must be in cookies. Do print_r($_COOKIES); and you will see that iPad/iPhone for some reason does not want to send you cookies, then we'll see.
You are not able to get the session on the iPhone because cookie is disabled.
Please go to Safari>Settings>Accept Cookies in your iPhone and set it to accept from Visited.
Then you will be able to create the session in PHP.
Check the session_id()
in both scripts. Most likely the session cookie is not being stored by the iPhone for some reason, so a new session is created each time the second page is created, which won't have the id
stored from the previous session.
Use some kind of wire sniffer on the iphone (or on the server if you can't do it on the phone) to see if the appropriate cookie headers are going back and forth.
The answer to your question is easy.
$_SESSION['id'] is read-only.
If you do $_SESSION['id'] = 'foo';
the next page it will revert back to its original value.
To change the session id:
session_id('foo');
It is imperative that you call session_id('foo');
before session_start()
.
精彩评论