PHP - 成功登录时不重定向

时间:2013-11-05 09:12:54

标签: php html mysql

这是一个奇怪的问题 当我进入login.php并在我的测试服务器上输入我的登录详细信息时,我会被重定向到admin.php页面,我应该这样做,

然而,当我将我的文件上传到我的托管服务器并且我转到login.php并输入我的登录详细信息时,没有发生,我的意思是页面只是重新加载。我应该像我在localhost上测试我的网站时那样重定向到admin.php页面,但是在webserver上没有任何反应

有谁知道为什么会这样? 这是我的代码:

//Open SQL connection
include 'connect.php';
//Check if user has entered Data yet
if(isset($_POST['submit']))
{   
//Get values from username and password user entered into form field
    $uname = $_POST['name'];
    $pword = $_POST['pword'];
//Get username and password from database   
    $result = mysql_query('SELECT `username`, `password` 
                            FROM player_info
                            WHERE `position` = "coach" ') or die (mysql_error());   
        while($row = mysql_fetch_array($result))
        {
            $username = $row['username'];
            $password = $row['password'];       
        }
//Check if username and password entered matched the username and password stored in the database
            if($uname != $username  || $pword != $password)
            {
            echo '<center><p style="color:red">Username and Password incorrect. Please try again</center>';
            }

//if username and password match redirect to admin page         
                else if($uname = $username && $pword == $password)
                {
                            echo("<script>location.href ='admin.php'</script>");
                    session_start();
                    $_SESSION[$pword];
                    $_SESSION['username'] = $username;  
                }
            }
            echo'<ul>';
            echo'<li><a href="login.txt"><b>Source Code</a>';
            echo'</ul>';        
//Login Form    
        echo'<form name="test" action="" method="post">';
                echo'<div align="center">';
                echo '<form name="login" method="post" action="">';
                echo'<input type="text" name="name">'; echo'<input type="password" name="pword">';
                echo '<input type="submit" name="submit">';
        echo '</form>';
echo '<h2>Welcome to the Scoregasims rugby club</h2>';
echo '<img src="images/logo.jpg">';
echo'</div>';

echo'
</body>
</html>';
?>

P.S我知道这个代码对于sqlinjections来说是不安全的,但我还没有了解它。

6 个答案:

答案 0 :(得分:1)

用户标题如:

header("location:admin.php") instead of js below 
 echo("<script>location.href ='admin.php'</script>");

你也可以像这样使用html重定向:

echo "<meta http-equiv='refresh' content='0;url=admin.php'>";

答案 1 :(得分:0)

请查看以下部分:

else if($uname = $username && $pword == $password)
{
echo("<script>location.href ='admin.php'</script>");
session_start();
$_SESSION[$pword];
$_SESSION['username'] = $username;  
}

您在上面的代码中回显了重定向。如果您想在成功登录后重定向并将值保存到admin.php的会话中,只需将此行$_SESSION['username'] = $username;下面的行写为

else if($uname = $username && $pword == $password)
{        
$_SESSION[$pword];
$_SESSION['username'] = $username;  
echo("<script>location.href ='admin.php'</script>");
}

我们必须在脚本顶部写session_start();

答案 2 :(得分:0)

停止“回应”HTML的每一行 - 你不可能编写和调试HTML。

 <?php
      some code...
      ... more code...
    ?>
      <div>Hello</div>
      <div><?php echo $user_name; ?></div>
    <?php ...more code... ?>

由于您需要指定标记中的脚本,因此您的内容已被破坏。做<script type="text/javascript">js code here</script>

最后,不要为此回应js。只需执行此PHP代码: header(Location: /admin.php);

答案 3 :(得分:0)

您的代码中存在错误 - else if($uname = $username && $pword == $password) ..您没有根据具体情况检查$username任何记录。我确定你的意思是使用==而不是而不是只是

答案 4 :(得分:0)

首先:你指定了两个表格。一个在另一个里面。

第二:应该正确使用标头标签。

P.S:你不需要回应每一个html。您可以简单地执行以下操作。

我已经修改了你的答案:

<?php
ob_start();
//Open SQL connection
include('connect.php');
//Check if user has entered Data yet
if(isset($_POST['submit'])){   
    //Get values from username and password user entered into form field
    $uname = $_POST['name'];
    $pword = $_POST['pword'];
    //Get username and password from database   
    $result = mysql_query('SELECT `username`, `password` FROM player_info WHERE `position` = "coach" ') or die (mysql_error());   
    while($row = mysql_fetch_array($result)){
        $username = $row['username'];
        $password = $row['password'];       
    }
    //Check if username and password entered matched the username and password stored in the database
    if($uname != $username  || $pword != $password){
    echo '<center><p style="color:red">Username and Password incorrect. Please try again</center>';
    }
    //if username and password match redirect to admin page         
    else if($uname = $username && $pword == $password){
        // echo("<script>location.href ='admin.php'</script>");
        session_start();
        $_SESSION[$pword];
        $_SESSION['username'] = $username;  
        header("Location:admin.php");
    }
}

?>
<html>
<head>
</head>
<body>
<ul>
    <li>
        <a href="login.txt"><b>Source Code</b></a>        
    </li>
</ul>      
<!-- Login Form     -->
<div align="center">
    <form name="login" method="post" action="">
        <input type="text" name="name">    
        <input type="password" name="pword">
        <input type="submit" name="submit">
    </form>
    <h2>Welcome to the Scoregasims rugby club</h2>
    <img src="images/logo.jpg">';
</div>


</body>
</html>

干杯

答案 5 :(得分:0)

in else if condition,

 else if($uname = $username && $pword == $password)

应该像

else if($uname == $username && $pword == $password) // equal to symbol is missed

部分应该是,

   else if($uname == $username && $pword == $password)
            {
                session_start();                    
                $_SESSION['username'] = $username;  
                $_SESSION['pword'] = $pword;  
                echo ("<script>window.location.href ='admin.php';</script>");

            }