8

I'm using bluehost as my webhost, I'm having a few problems. The code below is my point of confusion.

<?php
include '../init.php';
error_reporting(0);
?>
<div class='container'>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/bootstrap/css/responsive.css">    
<link rel="stylesheet" type="text/css" href="/bootstrap/css/responsive.min.css">
<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="SignIn.css">
<title>SoundBooth - Sign in</title>
<link rel="icon" href="../Images/Logo.png" type="image/x-icon"/>
</head>
<body  background="../Images/BGMain.png">
<div class='signinLabel'>Sign in</div>
<div class='bg'></div>
<div class='user'>Username</div>
<div class='pass'>Password</div>
<form action="index.php" method='POST'>
  <input name='Username' autocomplete="off"class='usr' type="text" style='height:34px;'/>
  <input name='Password' autocomplete="off"class='pas'type="password" style='height:34px;'/>

  <input type='submit' class='submit' value='Log in' ></input>
</form>
<a class='forgotPass' href="#">(Forgot password)</a>
<div class='no-account-label'>Don't have an account? |</div>
<a class='no-account-button' href="/signup">Sign up</a>
<?php
 if (empty($_POST) === false){
    $username = $_POST['Username'];
    $password = $_POST['Password'];

    if (empty($username) || empty($password)){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">You need to enter a username and password!</div>
            <?php
        }elseif (user_exists($username) === false){?>
            <div style="float:left;margin:-415px 170px;width:500px;text-indent:100px;"class="alert alert-error">We can't find that username! Have you <a style='color: rgb(185, 74, 72);' href='../HTML/signup.html'>registered?</a></div>
            <?php
        }elseif (user_active($username) === false){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">You haven't activated your account!</div> <!--Remove this-->
            <?php
        }else{

            $login = login($username, $password);
            if ($login === false){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">The username or password you have entered is incorrect!</div>
            <?php
            }else{
                $errors[] = 'Logging In...';
                $_SESSION['user_id'] = $login;
                if (isset($_SESSION['user_id'])){
                mysql_query("UPDATE `users` SET `online_offline`='1' WHERE `user_id`='$_SESSION[user_id]'");
                }
                header("Location: http://sound-booth.com/");
                exit();
            } 
        }
    }
    echo output_errors($errors)
?>
</div>
</body>
</html>

is the link to the script. It works in localhost, but not in my actual server. Not even the header() redirect works.

Could somebody please explain this?

Solution:

ob_start();
4

11 回答 11

9

最好的选择是像这样使用。

// Use this line instead of header
echo "<script>location='your_url.com'</script>";
于 2015-02-20T08:02:21.337 回答
7

Headers must be set before any output is sent. You are sending a lot of HTML to the user before doing that header() call. Perhaps its working in one environment but not in another because output_buffering is set on your localhost php.ini but not on the server.

You should rework your code to make sure header() is sent before any stuff, but if you want to just get it working append ob_start() to the beginning of your code, it will enforce the buffering.

于 2013-03-14T00:09:03.190 回答
4

ob_start();紧随其后<?phpob_end_flush();在文档末尾使用 。

于 2015-07-15T06:14:01.913 回答
2

HTTP headers must be sent before the HTTP response. I.e., you must call header before echoing anything else or sending any HTML.

Chances are, the reason it works on your local server is because you have configured output buffering there. You can insert a call to ob_start(); at the top of the page to enable output buffering within the script. Then, since the HTML will be buffered until the script is done, the redirect header can get to the browser first, as it must. Or, you can rework the script to put the header call at the top.

于 2013-03-14T00:07:25.093 回答
1
header("Location: http://sound-booth.com/");

it goes after text output.

Use OB_start() or change your code.

于 2013-03-14T00:07:55.610 回答
1

一个页面使用的第一行

<php ob_start();?>

然后在头文件下面使用

ob_end_flush();

例如像这样。

   <?php
ob_start();
?>
<?php
//Get values from the post Operation
require_once 'db/conn.php';

    if (isset($_POST['submit'])) {

        //extract values from the $_POST array
        $id = $_POST['id'];
        $fname = $_POST['firstName'];
        $lname = $_POST['lastName'];
        $dob = $_POST['dob'];
        $email = $_POST['email'];
        $contact = $_POST['phone'];
        $specialty = $_POST['specialty'];

        //call crud Funtion
        $result = $crud->editAttendees($id, $fname, $lname, $dob, $email, $contact, $specialty);
        //redirect  to index.php
        if ($result) {
        header("Location: viewrecords.php");
        ob_end_flush();
        
        } else {
            echo "error";
        }
    } 
于 2021-08-09T12:06:48.197 回答
0

我一直面临同样的问题。最后一个答案对我有用。

echo "location=' https://mysiteurl.in '";

感谢 Jon Surrell。它甚至还带有会话值。

于 2015-03-26T23:57:20.780 回答
0

You probably have your error reporting turned off which is why you won't get an error message.

header() does not work if you have even a white space before it. You would need to put it above all your string outputs in order for it to work.

于 2013-03-14T00:06:09.757 回答
0

操作,

您在开始标签<div class='container'> 之前<html>有(然后在</body>.

<div class='container'>打开<body>标签后尝试移动。

希望有帮助。

于 2013-03-14T00:11:53.753 回答
0
$r=mysqli_query($con,$stmt);
if(!$r)
{
    die("Server failed" . mysqli_error($con));
}
else{
header("Location: index.html");
exit();
}?>

上面的代码对我有用。

于 2018-05-25T07:41:54.380 回答
-1

我认为ob_start()函数调用是解决这个问题的最佳方法。头函数可以在逻辑语句之后调用他的属性。这是传递值,只是取它以前的值。

参考:https ://www.php.net/manual/en/function.ob-start.php

于 2020-02-16T13:35:01.617 回答