PHP重定向在服务器上不起作用 [英] PHP redirect not working on server

查看:67
本文介绍了PHP重定向在服务器上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PHP 和 Mysql 构建了一个 Web 应用程序.它有一个登录部分.它工作正常,在我的本地服务器上一切正常.但是当我在实时服务器上上传这个应用程序时,重定向功能不再起作用.我的代码:

I built a web application using PHP and Mysql. It has a login Section. It is working fine and everything ok on my local server. But when I uploaded this app on live server the redirect function is not working any more. My code:

登录.php:

<form action="do_login.php" method="post">
    <input name="user" type="text" id="user" size="25" placeholder="Username" />
    <input name="password" type="password" id="password"  size="25" placeholder="Password" />
<input name="submit" type="submit" value="Login" class="submit"/>
</form>

do_login.php:

do_login.php:

<?php
include 'includes/dbConnect.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());
$num_rows = mysql_num_rows($result);
$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;
    }
?>

当用户成功登录时,浏览器转到 do_login.php 并显示一个空白页面,该页面不会重定向到实时服务器上的 admin.php,而是在本地主机上重定向admin.php.当我手动转到 admin.php 时,它有限制,因此只有登录用户才能访问它,我可以访问它,这意味着我已经登录并且工作正常.

When a user login successfully the browser takes to do_login.php and displays a blank page that does not redirect to admin.php on live server but on localhost it redirect to admin.php. When I manually go to admin.php, which has restrictions so that only logged in users can access it, I can access it that means I have been logged in and works fine.

错在哪里?

推荐答案

ob_*

<?php

ob_start();

//Rest of your code

ob_end_flush();
?>

更新:这就是我建议使用 ob_* 函数的原因.我从 brian.moonspot.net 复制的

Updated: Here is the reason i suggested ob_* functions. I copied it from brian.moonspot.net

ob_start 的工作原理

那么,ob_start 有什么帮助呢?ob_start 中的 ob 代表输出缓冲.ob_start 将缓冲输出 (HTML),直到页面完全完成.页面完成后,将发送标头,然后发送输出.这意味着对 setcookie 或 header 函数的任何调用都不会导致错误,并且会正确发送到浏览器.您确实需要在任何输出发生之前调用 ob_start.再开始输出就晚了.

So, how does ob_start help? The ob in ob_start stands for output buffering. ob_start will buffer the output (HTML) until the page is completely done. Once the page is completely done, the headers are sent and then the output is sent. This means any calls to setcookie or the header function will not cause an error and will be sent to the browser properly. You do need to call ob_start before any output occurs. If you start output, it is too late.

这篇关于PHP重定向在服务器上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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