duankuangxie9070 2015-05-08 18:58
浏览 63

PHP + AJAX登录页面:在移动设备上需要两次登录

I'm new to programming, and I've created a simple login page - below is the stripped down version without security measures.

<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">

The data is handled through AJAX:

$.ajax( {
    type: 'POST',
    data: $( this ).serialize(),
    url: 'login.php'
} )

.done( function( data ) {
    if( data == 'login' ) {
        location = 'main/'
    }
} )

Which sends it to a PHP script (login.php). After validating the credentials a cookie is set:

setcookie( 'my_login_page', '12345678', time() + ( 60 * 60 * 24 * 365 ), '/' );

echo 'login';

It works perfectly, but after a while I noticed you have to login twice on mobile devices. The cookie 'disappears'. When you login again, it sticks. For weeks I've been trying to track down the cause, linked an iPhone to Safari for inspection, tried several different approaches for the redirection and more. For example location.reload( true ); instead of redirecting and redirecting with PHP if a cookie was set. Nothing seems to work and I can't find an issue on here that's exactly similar.

Can someone help me out? Maybe this isn't even the right approach for such a login, echoing 'login' feels awkward for example. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqd82461 2015-05-08 19:22
    关注

    Are you sure the cookie is disappearing? It sounds like what's happening is mobile safari is refreshing before PHP finishes setting the cookie, so it's not getting the cookie in the header until the next request. What happens when you login once with mobile and then refresh the page?

    评论

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)