PHP Cookie将不会设置,直到页面重新加载TWICE。这是怎么回事? [英] PHP cookie will not set until the page reloads TWICE. What's going on?

查看:194
本文介绍了PHP Cookie将不会设置,直到页面重新加载TWICE。这是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个奇怪的。我试图处理我的第一个登录系统,所以忍受我。



有2页。 PageA.php具有登录表单。填写表单并提交到PageB.php。 PageB对数据库运行检查,检索结果,并设置用户名值的cookie。 pageB然后使用头部('location:PageA.php')重定向回PageA,其中页面应该HYPOTHETICALLY看到有一个设置的cookie,并改变做一些无用的和愚蠢的(即回显欢迎回来,用户名)。但问题在这里。一切都按照计划,除了那个坏的欢迎回来的声明。由于某些原因,Cookie未设置。或者我想。然后我回到url地址,重新输入在我的浏览器中的url,按回车,然后voila。该cookie现在被识别,一切工作美丽。



此外,如果你只是简单地登录,登录ALSO就可以了。输入信息两次(即,它第一次失败,然后你立即再次在PageA上,然后它工作)。以下是相关代码:



PageA.php:

  ?php if(empty($ _ COOKIE ['user'])){?> 
< form action =PageB.phpmethod =get>

< input onfocus =this.value =''; maxlength =35type =textclass =usernamevalue =Usernamename =username/>

< input onfocus =this.value =''; maxlength =12type =passwordclass =usernamevalue =Passwordname =password/>


< input type =imageid =loginsrc =Images / home / login_button.png/>
< / form>
<?php}
else {

echo< div id ='welcome'> Welcome,。$ _ COOKIE ['user']。 ; / div>

;
}?>

PageB.php:



> $ query =SELECT * FROM users where username ='。$ user。'and password ='$ pw。LIMIT 1;
$ result = mysql_query($ query,$ con);

$ num_rows = mysql_num_rows($ result);

if($ num_rows> 0){

while($ row = mysql_fetch_array($ result)){

$ username = $ row [ '用户名'];

}

if(isset($ _ COOKIE ['user'])){

setcookie('user',$ username,time )-2000);

}

setcookie('user',$ username,time()+ 3600 * 168,/);

}
mysql_close();

header('location:PageA.php');

甚至更陌生的是,我没有在localhost上的问题,只有当它被放在网上)。任何洞察将非常感谢!非常感谢。



编辑:我忘了提及是索引(实际上是index.php)。我的头(位置)实际上重定向到url(例如头('location:http://www.domainname.com')。但是我现在得知,整个过程工作时,我重定向到index.php而不是域名。

解决方案

你的代码对我来说看起来不错。 可能会被浏览器缓存,请尝试向 PageA 添加一些附加标题(请记住在输出其他内容之前添加):

  header('Cache-Control:no-cache,must-revalidate'); 
header('Expires:Mon,26 Jul 1997 05:00 :00 GMT');

他们应该阻止浏览器缓存。






此外,您在一个请求中(在某些情况下)更改同一个Cookie两次:

  if(isset($ _ COOKIE ['user'])){

setcookie('user',$ username,time() b
$ b}

setcookie('user',$ username,time()+ 3600 * 168,/);

这应该没有问题,但没有意义 - 第一个请求将被第二个一个。


Okay, here's a strange one. I'm attempting to deal with my first login system so bear with me.

There are 2 pages. PageA.php has the login form. Fill out the form and submit to PageB.php. PageB runs check against database, retrieves results, and sets cookie with value of username. PageB then uses header('location:PageA.php') to redirect back to PageA where PageA should HYPOTHETICALLY see that there is a set cookie, and change to do something useless and stupid (i.e. echo "Welcome back, username"). BUT the problem is here. Everything goes according to plan, except for that damn welcome back statement. For some reason, the cookie did not set. Or so I think. Then I go back to the url address, retype the url in my browser, hit enter, and then voila. The cookie is now recognized and everything works beautifully. It even works such that if you were to then logout and log back in, you would not need to do the reload.

In addition, the login ALSO works if you simply enter the information twice (i.e. it fails the first time, then you do it again immediately on PageA and it then works). Here's the relevant code:

PageA.php :

    <?php if(empty($_COOKIE['user'])){?>
        <form action="PageB.php" method="get">

        <input onfocus="this.value='';" maxlength="35" type="text" class="username" value="Username" name="username" />

        <input onfocus="this.value='';" maxlength="12" type="password" class="username" value="Password" name="password"/>


        <input type="image" id="login" src="Images/home/login_button.png" />
        </form>
        <?php }
        else {

            echo "<div id='welcome'>Welcome, ".$_COOKIE['user']."!</div>

        ";
        }?>

PageB.php :

$query="SELECT * FROM users where username='".$user."' and password='".$pw."' LIMIT 1";
$result=mysql_query($query,$con);

$num_rows = mysql_num_rows($result);

if($num_rows>0){

    while($row = mysql_fetch_array($result)){

        $username=$row['username'];

    }

    if(isset($_COOKIE['user'])){

        setcookie('user',$username,time()-2000);

    }

    setcookie('user',$username,time()+3600*168,"/");

}
mysql_close();

header('location:PageA.php');

What's even stranger is that I have no issues on the localhost, only when it is put online (Bluehost). Any insight would be greatly appreciated! Thank you in advance.

EDIT: I had forgotten to mention that PageA is the index (so actually named index.php). My header(location) actually redirects to the url (e.g. header('location:http://www.domainname.com'). However I now learned that the whole process works when I redirect to index.php instead of the domain name. But now I have that ugly "/index.php" in the url.

解决方案

Your code looks OK to me. Output of PageA probably gets cached by a browser. Try adding some additional headers to PageA (remember to add them before anything else is outputted):

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

They should prevent browser from caching.


Also, you are changing the same cookie twice in one request (in some cases) here:

if(isset($_COOKIE['user'])){

    setcookie('user',$username,time()-2000);

}

setcookie('user',$username,time()+3600*168,"/");

This should be no issue but it doesn't make sense - first request will be overwritten by the second one.

这篇关于PHP Cookie将不会设置,直到页面重新加载TWICE。这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆