PHP header not working in MAMP even after removing white space

喜夏-厌秋 提交于 2019-12-23 04:26:28

问题


I am using MAMP and for some reason I am not getting redirected using header in php, can someone please have a look at the following code if there is something wrong in it???

I have read similar posts and found that it is caused by a sort of white space before header tag but in my case it is not.

Please give your advices...thanx

<?php

$host="localhost"; // Host name 

$username="root"; // Mysql username 

$password="root"; // Mysql password 

$db_name="DBase"; // Database name 

$tbl_name="customers"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from the form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$myusername = stripslashes($myusername);
$mypassword = md5($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE Username='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword"); 
header("location:/Account/index.php");
}
else {
echo "Wrong Username or Password.";
}
?>

回答1:


header('Location: http://example.com/Account/index.php');
exit;

is one way to do this but you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.

EDIT AND APPEND SOLUTION: ------>

You can use Output Buffering as

    <?php
    ob_start();
    $host="localhost"; // Host name 

$username="root"; // Mysql username 

$password="root"; // Mysql password 

$db_name="DBase"; // Database name 

$tbl_name="customers"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from the form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$myusername = stripslashes($myusername);
$mypassword = md5($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE Username='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword"); 
header("location:/Account/index.php");
}
else {
echo "Wrong Username or Password.";
}
    ob_end_flush();
    ?>

The problem is that we cannot send the header after we start sending the output. To solve this we buffer the output. The function ob_start turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. So the echo output will be buffered. Next we send the header without any problem as we've not yet spit out any output. Finally we call ob_end_flush to flush the internal buffer contents and to stop output buffering.




回答2:


I had the same problem in MAMP and it turned out that output_buffering was disabled in my php.ini configuration file. All I had to do is turn it back on.

in php.ini find this line:

output_buffering = Off

and change it to:

output_buffering = 4096

Then restart MAMP

Please see this post for more details: http://hibbard.eu/php-headerlocation-not-working-in-mamp/




回答3:


It should be something like this:

header('Location: http://example.com/Account/index.php');
exit;


来源:https://stackoverflow.com/questions/12033257/php-header-not-working-in-mamp-even-after-removing-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!