PHP:必须登录两次,直到设置会话变量 [英] PHP : Must login twice until session variables are set

查看:126
本文介绍了PHP:必须登录两次,直到设置会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:经过许多小时的搜索后,当我访问我的网站而未添加www时,似乎出现了此问题。在域名之前。所以实际发生的情况是,我正在使用example.com/login.php设置会话的某处登录,以致于我的成员控件无法识别,因此它会将我重定向到www.example.com/login.php,我登录了一切正常。



当我从www.example.com/login.php(使用www)登录时,它从第一次尝试正确登录。 p>

因此,我添加了一个代码,以确保我始终在URL中包含www:

  if($ _SERVER ['HTTP_HOST'] ==example.com)
{
$ url =http:// www。 。 $ _SERVER ['HTTP_HOST']。 $ _SERVER [ REQUEST_URI];
header(Location:$ url);
}

现在一切正常。希望它可以帮助别人。






所以,我建立了3个网站,并且都有同样的问题,知道为什么,我必须登录两次,直到我有条件登录..(直到会话变量设置)。帮助真的很感激,一直试图解决这个问题,并寻找解决方案,因为很长一段时间...

$ p $ session_start();
if((isset($ _ SESSION ['UserName']))&(isset($ _ SESSION ['LastActivity'])))
{
header('Location:http ://www.example.com/Account.php');

$ b $ if(isset($ _ POST ['username']))
{
mysql_connect(localhost,DBuser,pass)或
die(无法连接到mysql);
mysql_select_db(DBNAME)或die(no database);

$ inputUserName = $ _POST ['username'];
$ inputPass = $ _POST ['password'];

$ datausername = mysql_real_escape_string($ inputUserName);
$ password = md5($ inputPass);

$ sqlCommand =SELECT * FROM Members
WHERE UserName ='$ datausername'AND
Password ='$ password';

$ result = mysql_query($ sqlCommand);

$ b $ if(mysql_num_rows($ result)> 0)
{
$ _SESSION ['UserName'] = $ datausername;

$ _SESSION ['LastActivity'] = time();
sleep(2);
$ LoginDate = date('Y-m-d H:i:s');

mysql_connect(localhost,DBUPDATEusername,DBuserPass)或
die(无法连接到mysql);
mysql_select_db(databaseName)或死(no database);

mysql_query(更新成员SET LastLogin ='$ LoginDate'WHERE
UserName ='$ datausername');

mysql_close();
echo'< meta http-equiv =Refreshcontent =0; url = http://www.example.com/Account.php?p = Login_Success/>';
}
else {
mysql_close();
echo'< div id =error_msg>错误:输入的信息不正确。 < / div>';}
}

}
?>请检查并再试一次。

登录表单(with action =),以及方法后)。
$ b 注意:我使用html刷新标记,因为我不能使用标题重定向..(我得到的错误是标题已经发送)。



以及example.com/Account.php我会在代码顶部检查:

  session_start(); 
if((!isset($ _ SESSION ['UserName']))||(!isset($ _ SESSION ['LastActivity'])))
{
header('Location:http ?://www.example.com/ p = Must_Login');
}

而且这似乎是我第一次登录并重定向到account.php面板..会话值没有设置,并将我重定向到Must_Login页面。我再次登录(我第一次使用的登录页面相同,但是第二次登录时,它确实设置了会话值,并且一切正常。)



谢谢非常感谢您的帮助!

解决方案

解决方案:经过数小时的搜索,似乎出现了这个问题,当我访问我的网站没有在域名前添加www,所以实际发生的情况是,我在某处登录了mydomain.com/login.php设置会话,我的会员控制无法识别,因此它将我重定向回到www.mydomain.com/login.php,当我登录一切工作正常。



当我从www.mydomain.com/login.php登录(与所以我添加了一个代码,以确保我始终在URL中包含www:

$ b $($)

b

  if($ _SERVER ['HTTP_HOST'] ==mydomain.com)
{
$ url =http:/ / www。。$ _SERVER ['HTTP_HOST']。$ _SERVER [ REQUEST_URI];
标题( 位置:$ URL);
}

现在一切正常。希望它能帮助别人。


SOLUTION: after many hours of searching, it seems this problem was occurring when I access my website without adding the "www." before the domain. so what actually was happening is, I was logging in with example.com/login.php sets session somewhere, that my member control doesn't recognize, so it redirects me back to www.example.com/login.php, that when I login everything works Ok.

when I login from www.example.com/login.php (with the www.) it logs in correctly from first attemp.

So I added a code to make sure I always have the www in the URL:

if ($_SERVER['HTTP_HOST'] == "example.com")
{
   $url = "http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header("Location: $url");
} 

and everything works well now. Hope it helps someone.


So, I have built over 3 websites, and all have same problem, I don't know why, I have to login twice till I'm rly logged in.. (till the session variables are set). Help is really appreciated, been trying to fix this and looking for solution since a long time...

    session_start();
if ((isset($_SESSION['UserName']))&&(isset($_SESSION['LastActivity'])))
{
   header ('Location: http://www.example.com/Account.php');
}

if (isset($_POST['username']))
{
    mysql_connect("localhost","DBuser","pass") or 
    die ("could not connect to mysql");
    mysql_select_db("DBNAME") or die ("no database");

    $inputUserName = $_POST['username'];
    $inputPass = $_POST['password'];

    $datausername = mysql_real_escape_string($inputUserName);
    $password=md5($inputPass);

    $sqlCommand = "SELECT * FROM Members 
    WHERE UserName='$datausername' AND 
    Password='$password'";

    $result = mysql_query($sqlCommand);


    if (mysql_num_rows($result) > 0)
    {
       $_SESSION['UserName'] = $datausername;

       $_SESSION['LastActivity']= time();
       sleep(2);
       $LoginDate = date('Y-m-d H:i:s');

       mysql_connect("localhost","DBUPDATEusername","DBuserPass") or 
    die ("could not connect to mysql");
    mysql_select_db("databaseName") or die ("no database");   

       mysql_query("Update Members SET LastLogin='$LoginDate' WHERE 
            UserName='$datausername'");

       mysql_close(); 
       echo '<meta http-equiv="Refresh" content="0;url=http://www.example.com/Account.php?p=Login_Success"/>';
    }
    else {
        mysql_close();
        echo '<div id="error_msg">Error: Information entered are not correct. Please check and try again.</div>';}
    }

}
?>
<form...

login form (with action=""), and method post).

Note: I use the html refresh tag, because I can't use the header redirect.. (I get error that header is already sent).

and in the example.com/Account.php I do this check at the top of the code:

  session_start();
if ((!isset($_SESSION['UserName']))||(!isset($_SESSION['LastActivity'])))
{
    header('Location: http://www.example.com/?p=Must_Login');
}

And and it seems that first time I login and am redirected to account.php panel.. the session values are not set, and redirects me back to Must_Login page. I login again (same login page I use at first time. But the second time when I login, it does set the session values, and everything is OK.

Thank you very much for your help in advance!

解决方案

SOLUTION: after many hours of searching, it seems this problem was occurring when I access my website without adding the "www." before the domain. so what actually was happening is, I was logging in with mydomain.com/login.php sets session somewhere, that my member control doesn't recognize, so it redirects me back to www.mydomain.com/login.php, that when I login everything works Ok.

when I login from www.mydomain.com/login.php (with the www.) it logs in correctly from first attemp.

So I added a code to make sure I always have the www in the URL:

if ($_SERVER['HTTP_HOST'] == "mydomain.com")
{
   $url = "http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header("Location: $url");
} 

and everything works well now. Hope it helps someone.

这篇关于PHP:必须登录两次,直到设置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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