在iPhone网络应用程序上维护PHP会话 [英] Maintaining a PHP session on an iPhone web app
问题描述
我刚刚在我的iPhone上创建了一个网络应用程序。我可以打开它,没有问题,登录,但每当我返回到应用程序,它忘记了我的上一个会话,我必须重新输入我的用户名和密码。
I've just created a web app on my iPhone. I can open it and log in with no problem, but whenever I return to the app it has forgotten my previous session and I have to re-enter my username and password.
有关于此的一些其他问题,但他们的答案没有帮助我解决这个问题,因为我不知道放在哪里提供的PHP。
There have been a few other questions about this, but their answers haven't helped me to solve the problem because I'm not sure where to put the PHP that was provided.
这是我找到的最佳答案:在iPhone上的网络应用中维护PHP会话
This is the best answer that I've found: Maintain PHP Session in web app on iPhone
在答案中,Wilbo Baggins(http://stackoverflow.com/users/346440/wilbo-baggins )提供了以下代码:
In the answer, Wilbo Baggins (http://stackoverflow.com/users/346440/wilbo-baggins) provides the following code:
// Start or resume session
session_start();
// Extend cookie life time by an amount of your liking
$cookieLifetime = 365 * 24 * 60 * 60; // A year in seconds
setcookie(session_name(),session_id(),time()+$cookieLifetime);
我在<?php
和?>
标签,但是没有解决问题。我猜我把它放在错误的地方,所以我寻找指导,将解释我应该把它放在哪里。
I entered that code between <?php
and ?>
tags in the website's header, but that hasn't solved the issue. I'm guessing that I'm putting it in the wrong place, so I'm looking for guidance that will explain where I should actually be putting it.
谢谢。
-
BUMP:有没有人可以帮助我解决这个问题,或者,至少,我知道我如何能够与Wilbo Baggins联系( http://stackoverflow.com) / users / 346440 / wilbo-baggins )?
BUMP: Is there anyone out there who can help me solve this issue or, at the very least, knows how I can get in touch with Wilbo Baggins (http://stackoverflow.com/users/346440/wilbo-baggins)?
推荐答案
在您的login.php中,添加以下代码登录信息正确:
In your login.php, add the following code when login information correct:
session_start();
//your code here
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),"apple")) //to prevent cookies for non-apple devices
{
$cookieLifetime = 365 * 24 * 60 * 60; // A year in seconds
setcookie("ses_id",session_id(),time()+$cookieLifetime);
}
然后,在每个其他* .php中,您希望恢复session_id,在session_start()之前添加以下行:
Then, in every other *.php you want your session_id to be restored, add the following line BEFORE session_start():
if($_COOKIE['ses_id']){
session_id($_COOKIE['ses_id']);
}
session_start();
在您的logout.php中,添加以下代码:
In your logout.php, add the following code:
session_start();
$cookieLifetime = 365 * 24 * 60 * 60; // A year in seconds
setcookie("ses_id","",time()-$cookieLifetime); //set lifetime to negative to autodelete cookie
session_destroy();
希望这有帮助。
这篇关于在iPhone网络应用程序上维护PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!