繁体   English   中英

标头函数未重定向到home.php,为什么?

[英]header function not redirecting to home.php, why?

在这里,我在登录后使用头函数重定向到home.php,但是头函数没有重定向到该页面。 即使当我在本地计算机上运行相同的代码时,它也能正常工作。

<?php
ob_start();
session_start();
require_once 'phpconnection.php';
// it will never let you open index(login) page if session is set
if ( isset($_SESSION['user'])!="" ) {
 header("Location:home.php");
 exit;
}
$error = false;
 if( isset($_POST['btn-logIn']) ) { 
// prevent sql injections/ clear user invalid inputs
 $email = trim($_POST['email']);
$email = strip_tags($email);
$email = htmlspecialchars($email);

$pass = trim($_POST['password']);
$pass = strip_tags($pass);
$pass = htmlspecialchars($pass);
// prevent sql injections / clear user invalid inputs
 if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
 $error = true;
 $errMsg = "Please enter valid email address.";
}
// if there's no error, continue to login
if (!$error) {
 $res=mysql_query("SELECT userId, userfName, userlName,userPassword FROM       userdata WHERE userEmail='$email'");
 $row=mysql_fetch_array($res);
 $count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row
 if( $count == 1 && $row['userPassword']==$pass  ) {
  $_SESSION['user'] = $row['userId'];
   header("Location:home.php");
 } else {
  $errMsg = "Try again...";
 }
}
}
 ?>

您不需要第5行的!=“”,因为isset()已检查存在性。 它的存在与否。

if (isset($_SESSION['user'])){
    header("Location: home.php");
    exit;
} else {
    echo "something here";
}

您也可以使用!isset()获得相反的结果。

使用此代码尝试您的代码,

<?php
ob_start();
session_start();

if ( isset($_SESSION['user'])!="" ) {
 header("Location:home.php");
 exit;
}

require_once 'phpconnection.php';
// it will never let you open index(login) page if session is set

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM