如何在PHP中重定向到页面?

时间:2021-01-11 21:17:41

I try to make a login form for my active page. The registration also completed. My problem is, that when I was get that I logged in successfully, I cannot to redirect to startpage with 'header(location:)'. Please help me, or fix the php's errors. Thanks!

我尝试为我的活动页面创建一个登录表单。注册也完成了。我的问题是,当我成功登录时,我无法使用'header(location :)'重定向到startpage。请帮帮我,或修复php的错误。谢谢!

Note: the links of the php are samples, aren't real.

注意:php的链接是样本,不是真实的。

<?php

$hostname="host"; // Host of MySQL
$user="user"; // Username at MySQL
$password="pass"; // Password at MySQL
$dbname="db"; // Database on MySQL

$connection=mysql_connect($hostname, $user, $password);
    if(! $connection)
        {
            die(''.mysqlerror());
        }
mysql_select_db($dbname, $connection) or die('');

$given_email=mysql_real_escape_string($_POST['email']);
$given_pass=mysql_real_escape_string($_POST['pass']);

$sql="SELECT * FROM users WHERE email='$given_email' and password='$given_pass'";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);

if($count==1)
{    
    //echo "OK";
    header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    //setUrl ("http://testseite.aufderlicht.bplaced.de/loggedin/start.html");
}
    else 
    {
        setUrl ("http://testseite.aufderlicht.bplaced.de/login/err/err.html");
    }

//mysql_close($connection)

?>

2 个解决方案

#1


0  

Sometimes I just put the '/' relative path in the header:

有时我只是将'/'相对路径放在标题中:

header ('Location: /');

However, this is the recommended:

但是,这是推荐的:

header ('Location: http://example.com/');

Doing only this won't work: header ('Location: '); , as can be seen in the specifications:

只做这个不起作用:header('Location:'); ,从规格中可以看出:

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

注意:HTTP / 1.1需要绝对URI作为»Location的参数:包括方案,主机名和绝对路径,但某些客户端接受相对URI。你通常可以使用$ _SERVER ['HTTP_HOST'],$ _SERVER ['PHP_SELF']和dirname()自己创建一个相对的URI:

Also, few notes:

还有几点说明:

  • Do NOT store passwords in plain text. Even thought this note won't fix your code, it's very important.

    不要以纯文本格式存储密码。即使认为本说明不会修复您的代码,也非常重要。

  • Include some error inside every die(''). As they stand, they only kill the script without providing any useful information. It could perfectly be that one of them is being triggered. For example:

    在每个骰子('')中包含一些错误。他们认为,他们只会在没有提供任何有用信息的情况下杀死脚本。完全可能是其中一个被触发了。例如:

mysql_select_db($dbname, $connection) or die('Could not select database');

mysql_select_db($ dbname,$ connection)或die('无法选择数据库');

  • Don't use mysql_* functions for new code since they are deprecated, PDO or mysqli_* are much better.
  • 不要将mysql_ *函数用于新代码,因为它们已被弃用,PDO或mysqli_ *要好得多。

#2


0  

You can do a redirection using Javascript instead of PHP page,especially because some webhosts don't allow using header() in the middle of files.

您可以使用Javascript而不是PHP页面进行重定向,尤其是因为某些webhost不允许在文件中间使用header()。

here is an example :

这是一个例子:

       echo "
         <script type='text/javascript'>
              window.location.replace('http://www.yoursite.com/new_page.html');

         </script>";

#1


0  

Sometimes I just put the '/' relative path in the header:

有时我只是将'/'相对路径放在标题中:

header ('Location: /');

However, this is the recommended:

但是,这是推荐的:

header ('Location: http://example.com/');

Doing only this won't work: header ('Location: '); , as can be seen in the specifications:

只做这个不起作用:header('Location:'); ,从规格中可以看出:

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

注意:HTTP / 1.1需要绝对URI作为»Location的参数:包括方案,主机名和绝对路径,但某些客户端接受相对URI。你通常可以使用$ _SERVER ['HTTP_HOST'],$ _SERVER ['PHP_SELF']和dirname()自己创建一个相对的URI:

Also, few notes:

还有几点说明:

  • Do NOT store passwords in plain text. Even thought this note won't fix your code, it's very important.

    不要以纯文本格式存储密码。即使认为本说明不会修复您的代码,也非常重要。

  • Include some error inside every die(''). As they stand, they only kill the script without providing any useful information. It could perfectly be that one of them is being triggered. For example:

    在每个骰子('')中包含一些错误。他们认为,他们只会在没有提供任何有用信息的情况下杀死脚本。完全可能是其中一个被触发了。例如:

mysql_select_db($dbname, $connection) or die('Could not select database');

mysql_select_db($ dbname,$ connection)或die('无法选择数据库');

  • Don't use mysql_* functions for new code since they are deprecated, PDO or mysqli_* are much better.
  • 不要将mysql_ *函数用于新代码,因为它们已被弃用,PDO或mysqli_ *要好得多。

#2


0  

You can do a redirection using Javascript instead of PHP page,especially because some webhosts don't allow using header() in the middle of files.

您可以使用Javascript而不是PHP页面进行重定向,尤其是因为某些webhost不允许在文件中间使用header()。

here is an example :

这是一个例子:

       echo "
         <script type='text/javascript'>
              window.location.replace('http://www.yoursite.com/new_page.html');

         </script>";