登录时重定向用户

时间:2016-07-14 01:20:12

标签: php html mysql database session

我正在尝试根据用户登录显示不同的标头。用户可以使用login.php登录。但是,当用户未登录或登录时,我无法显示header.php。我希望页面在登录时显示配置文件表单内容,反之亦然。有人可以帮忙吗?

<!DOCTYPE html>
<html>
    <head>
    </head> 
    <body>
        <?php
        require("header.php");
        ?>
        <div class = "container">
        //form content
        </div>
    </body>

</html>

的config.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "123";
$dbdatabase = "db";
$db = mysqli_connect($dbhost, $dbuser, $dbpassword) or die("couldn't connect to the Database");
mysqli_select_db($db, $dbdatabase) or die ("couldn't find db");
?>

的header.php

<?php
if(!isset($_SESSION)){
    session_start();
}
if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
    session_unset();
    session_regenerate_id();
}
include("config.php");
?>
!DOCTYPE html>
<html>
    <head>
    </head> 
    <body>
        <?php
    //if user is logged in, display navigation bar with user data
    if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE){ ?>
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
                //navigation - profile form content
            </nav>     
    <?php}
    //if user is not logged in, ask them to login
    else { ?>
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
                //user log in here
                //navigation - login form content
            </nav>

    <?php } ?>
    </body>

</html>

的login.php

<?php
session_start();
require_once("config.php");

if(isset($_SESSION['SESS_LOGGEDIN'])) {
header("Location: index.php");
die();
}


$useremail = $_POST['Lemail'];
$userpassword =  $_POST['Lpass'];

if ($useremail&&$userpassword) {

    $sql = mysqli_query($connect,"SELECT * FROM users WHERE userEmail='$useremail' and userpassword = $userpassword" );
    $numrows = mysqli_num_rows($sql);

    if ($numrows!=0) {

        while ($row = mysqli_fetch_assoc($sql)) {
            $_SESSION['SESS_LOGGEDIN'] = 1;
            $dbemail = $row['email'];
            $dbpassword = $row['password'];
            $dbname = $row['firs_Name'];
        }
        if ($useremail==$dbemail&&$userpassword==$dbpassword) {
              header("Location: index.php");
            $_SESSION['Lemail']= $useremail;
            die();

        }
        else
             header("Location: index.php");
            die();
    }
    else
        die("That user doesn't exist!");
}
else
    die("please enter username and password");

&GT;

2 个答案:

答案 0 :(得分:0)

根据评论建议修改了一些代码:

<?php
session_start();
require_once("config.php");
if(isset($_SESSION['SESS_LOGGEDIN'])) {
  header("Location: index.php");
  die();
}

$useremail = $_POST['Lemail'];
$userpassword =  $_POST['Lpass'];
$loginres = mysqli_query("SELECT * FROM users WHERE userEmail='$useremail'" );
$numrows = mysqli_num_rows($loginres);
if($numrows == 1)
{

  $loginrow = mysql_fetch_assoc($loginres);
  $_SESSION['SESS_LOGGEDIN'] = 1;
  $_SESSION['SESS_USEREMAIL'] = $loginrow['userEmail'];
  $_SESSION['SESS_USERPASS'] = $loginrow['userpassword'];
  $_SESSION['SESS_USERNAME']  = $loginrow['username'];
  header("Location: index.php");
  die();
}
else {
  header("Location: http://" .$_SERVER['HTTP_HOST']. $_SERVER['SCRIPT_NAME'] . "?error=1");
  die();
}

其他一些注意事项:如果您在文件中包含的所有内容都是PHP,则不要关闭?>,这样可以防止输出流入缓冲区。在此代码中,您永远不会检查用户的密码 - 可能是个好主意。最后,将用户密码存储为会话变量从来没有充分的理由。

答案 1 :(得分:-2)

我不赞成你,你可以ajax或goto。

&#13;
&#13;
$.ajax({
          type:"POST",
          async:false,
          url:"this.php",
          data:{url:"a"},
          beforeSend: function () {
            alertLoad('loding...');
          },
          success:function(GetRes){
               thisdom.html(GetRes);
          },
          error:function(){
               alertNew('error.');
          }         


  });
&#13;
&#13;
&#13;

this.php     <?php echo $this_data; ?>

相关问题