需要帮助重定向PHP

时间:2014-12-30 11:03:40

标签: php mysql

我制作了一个Web应用程序,在本地服务器上运行正常但是当我将它上传到我的Web服务器上时它给了我一个错误。 的login.php:

 <form action="do_login.php" method="post">
 <table>
 <tr>
<td width="256" align="center">Username:</td>
<td width="20"></td>
<td width="257"><input name="user" type="text" id="user" size="25" /></td>
</tr>

<tr>
<td align="center">Password::</td>
<td></td>
<td><input name="password" type="password" id="password"  size="25" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
  <input name="submit" type="submit" value="Login" id="submit_btn"/>
  <input name="reset" type="reset" value="Reset" id="reset_btn" />  </td>
</tr>
</table>
</form>

do_login.php:

<?php
include 'includes/dbConnect.php';  
include 'includes/authentication.php'; 

$my_user = $_POST['user'];
$my_password = $_POST['password'];

 if ($my_user == '' || $my_password == '')
    {
        $myURL = 'error.php?eType=pass';
        header('Location: '.$myURL);
        exit;
    }

 $result = mysql_query("SELECT * FROM users where username = '$my_user' and password =   '$my_password'") or die("SELECT Error: ".mysql_error());
  $get_info = mysql_fetch_row($result);

    if (mysql_num_rows($result) > 0)
    { 
        session_start(); 
        $_SESSION['login_status'] = "yes" ;
        $_SESSION['email'] = $get_info['3'];
        $_SESSION['full_name'] = $get_info['0'];

        $myURL = 'admin.php';
        header('Location: '.$myURL);
    }
   else
    {
        $myURL = 'error.php?eType=wrong';
        header('Location: '.$myURL);
        exit;
     }
    ?>

当用户登录时需要dologin.php,但不要将其重定向到admin.php。在本地服务器上,它对我来说很好,但现在我有问题。请指导我在哪里做错了。

2 个答案:

答案 0 :(得分:0)

在页面顶部开始您的会话

session_start(); 

如果您在会话开始之前发送任何输出,那么它会在标题重定向中产生问题。

否则您可以使用<meta http-equiv="Location" content="admin.php">它也可以用作标题重定向。

答案 1 :(得分:0)

使用ob_start()来阻止发送标头。

所以:

<?php

ob_start();

include 'includes/dbConnect.php';  
include 'includes/authentication.php'; 

$my_user = $_POST['user'];
$my_password = $_POST['password'];

 if ($my_user == '' || $my_password == '')
    {
        $myURL = 'error.php?eType=pass';
        header('Location: '.$myURL);
        exit;
    }

 $result = mysql_query("SELECT * FROM users where username = '$my_user' and password =   '$my_password'") or die("SELECT Error: ".mysql_error());
  $get_info = mysql_fetch_row($result);

    if (mysql_num_rows($result) > 0)
    { 
        session_start(); 
        $_SESSION['login_status'] = "yes" ;
        $_SESSION['email'] = $get_info['3'];
        $_SESSION['full_name'] = $get_info['0'];

        $myURL = 'admin.php';
        header('Location: '.$myURL);
    }
   else
    {
        $myURL = 'error.php?eType=wrong';
        header('Location: '.$myURL);
        exit;
     }
?>