登录后将PHP重定向到另一个页面

时间:2020-07-11 20:25:23

标签: php

即使if条件($ num> 0)为true,我也不会被重定向到welcome.php,其他开发人员也有帮助吗?

if(isset($_POST['login'])){
$password=$_POST['password'];
$email=$_POST['email'];
$ret= mysqli_query($con,"SELECT * FROM users WHERE email='$email' and password='$password'");
$num=mysqli_fetch_array($ret);

if($num>0)
{
$extra="welcome.php";
$_SESSION['login']=$_POST['email'];
$_SESSION['id']=$num['id'];
$_SESSION['full_name']=$num['full_name'];

$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("Location:http://$host$uri/$extra");
exit();
}

单击具有正确属性的登录按钮后,我卡在同一页面上,只是空白页面。

   <form name="login" action="" method="post">

          <div class="uk-margin">
              <div class="uk-inline">
                   <span class="uk-form-icon" uk-icon="icon: mail"></span>
                      <input class="uk-input" name="email" type="email" placeHolder="Enter your e-mail">
               </div>
           </div>
                    
           <div class="uk-margin">
               <div class="uk-inline">
                   <span class="uk-form-icon" uk-icon="icon: lock"></span>
                      <input class="uk-input" name="password" type="password" placeHolder="Enter your password">
               </div>
                            
           </div>
           <input type="submit" name="login" value="LOG IN" >

    </form>

2 个答案:

答案 0 :(得分:-1)

替换标题(“ location:http:// $ host $ uri / $ extra”); 带有标题(“位置:http:// $ host $ uri / $ extra”);

答案 1 :(得分:-1)

Betwen变量必须在带双引号或方括号的变量之前和之后为点。

$host.$uri
or {$host}{$uri}

第二件事,您已将变量插入字符串中。 这就是为什么它不起作用。

尝试一下:

$full_url = "http://".$host.".$uri.".$extra;
header("Location: ".$full_url);
die();

您还可以使用:

$full_url = "http://{$host}{$uri}{$extra}";
header("Location: {$full_url}");
die();

第三件事,我更喜欢在使用header(“ Location”);时使用die()而不是exit();

您可以在此问题中了解有关die()或exit()的更多信息。 PHP: Utilizing exit(); or die(); after header("Location: ");

基本上die()关闭所有内容,然后重定向无效。 如果使用exit(),如果某些脚本继续运行,您可能会被利用。