基于性别的登录重定向

时间:2018-01-23 20:21:52

标签: php mysqli

我希望在登录后根据性别重定向用户。 我想在登录后根据性别显示具体问题。

请在下面找到我用于注册用户的重定向代码(我在登录时尝试过,但我得到一个空白页):

if($_POST['gender']=='Couple MF')
{
 header('location:profileMF.php');

}
else if($_POST['gender']=='Couple MM')
{
 header('location:profileMM.php');
}
else if($_POST['gender']=='Couple FF')
{
 header('location:profileFF.php');
}
else if($_POST['gender']=='Male')
{
 header('location:profileM.php');
}
else if($_POST['gender']=='Female')
{
 header('location:profileF.php');
}
else if($_POST['gender']=='TV/TS')
{
 header('location:profileTT.php');
}
    //header("location: registration2.php");
    exit();
}
else
{

这是我到目前为止写的完整登录代码:

<?php
include("config/db_connect.php");
//if(isset($_POST['submit'])) {
$sdate      = date('d-m-Y h:i:s A');
$email      = $_POST['email'];
$password   = md5($_POST['pass']);

// User Login code
$login_check = mysqli_query($conn, "select * from user where user_email = '$email' and user_password = '$password' and status = '1' ") or die("fetch error");
if(mysqli_num_rows($login_check)>0)
{

    $logmysql_data          = mysqli_fetch_array($login_check);
    $_SESSION['userid']     = $logmysql_data['user_id'];
    $_SESSION['username']   = $logmysql_data['user_name'];
    $words                  = explode(' ', trim($logmysql_data['user_name']));
    $_SESSION['usernamechat'] = $words[0];

    $_SESSION['SESS_email'] = $logmysql_data['user_email'];

    // Update user last login
   $lastlog= mysqli_query($conn,"update user_lastlogin set lastlogin = '$sdate' , ipaddress = '".$_SERVER['REMOTE_ADDR']."' , online_status = '1' where user_id = '".$_SESSION['userid']."' ");

   //$_SESSION['start_date'] = $sdate;

     //echo $_SESSION['SESS_email']; die;

1 个答案:

答案 0 :(得分:0)

不要使用$ _GET,因为用户具有不同的个人资料背景。个人资料页面将公开,因此任何具有不同性别的人都可以访问它。

为了方便起见,我更喜欢使用会话变量作为参数。所以它不再需要从数据库中获取。如果用户更新了他的个人资料,您应该将更新值放入会话中。

在登录页面中,您应该添加:

  

$ _ SESSION ['gender'] = $ logmysql_data ['gender'];

注意:但在此之前,请在您的用户表格中创建一个新列

目前正在使用isset登录,因此请确保您的登录表单已完成。

<?php

            if(isset($_POST[login]))
            {
                $password = md5($_POST['pass']);
                $sdate      = date('d-m-Y h:i:s A');

                $login_check = mysqli_query($conn, "SELECT * FROM user WHERE user_email ='$_POST[email]' AND user_password = '$pass' AND status = '1'");

                $logmysql_data = mysqli_fetch_array($login);

                if (mysql_num_rows($login_check) > 0){

                    session_start();
                    $_SESSION['user_email'] = $logmysql_data['user_email'];
                    $_SESSION['user_id'] = $logmysql_data['user_id'];
                    $_SESSION['username']   = $logmysql_data['user_name'];

                    $words = explode(' ', trim($logmysql_data['user_name']));
                    $_SESSION['usernamechat'] = $words[0];  

                    // This new column of your database
                    $_SESSION['gender'] = $logmysql_data['gender'];

                    $lastlog = mysqli_query($conn,"update user_lastlogin set lastlogin = '$sdate' , ipaddress = '".$_SERVER['REMOTE_ADDR']."' , online_status = '1' where user_id = '".$_SESSION['user_id']."' ");

                    if ($lastlog)) {
                        // If update, goto Page2
                        header('location: Page2.php');
                    } else {
                        // If no update, put error here

                    }

                }
                else

                    echo "<script>alert('Sorry failed to login');</script>";
            }
        ?>

这是login.php(示例)(还没有测试)

<?php
include("config/db_connect.php");
session_start();


if (empty($_SESSION['gender'])){
    // If the user didnt update his gender, he wont get the profile page
    // Set error messages
    $error = "<span>You need to set your gender first!</span>";
    header("location: index.php");
}
else {
    // Using the Session as parameter or condition
    if ($_SESSION[gender] == "Couple_MF") {
        header("location: profile/couple_mf.php");
    }
    else if ($_SESSION[gender] == "Couple_MM") {
        // ..
    }
    else if ($_SESSION[gender] == "Couple_FF") {
        // ..
    }
    // .. Repeat
} 
?>

这是profile.php(示例)

小贴士:作为最后一部分。使用echo来显示html代码,而不是使用header()函数。