如果会话已经开始,如何忽略会话?

我有一个登录页面,登录后将其带到我的主页。如果用户不注销并稍后再回来,则无需再次登录。但是,如果用户已经登录,那么它将显示一条错误消息,指出会话已经启动,因此,它将忽略以下代码。


这是我的session.php文件。我想添加一个if语句,说如果会话已经启动,则将用户带到主页,如果会话未启动,则将用户带到登录页面。


<?php

// mysqli_connect() function opens a new connection to the MySQL server. 

$conn = mysqli_connect("localhost", "root", "", "supercar"); 

session_start();// Starting Session

// Storing Session 

$user_check = $_SESSION['login_user']; 

// SQL Query To Fetch Complete Information Of User 

$query = "SELECT * from adminlogin where email = '$user_check'"; 

$ses_sql = mysqli_query($conn, $query); 

$row = mysqli_fetch_assoc($ses_sql); 

$login_session = $row['nom'];

$login_session2 = $row['id'];

$login_session3 = $row['motdepasse'];


?>


尚方宝剑之说
浏览 220回答 4
4回答

互换的青春

试试这个代码:if(!isset( $_SESSION['login_user'])){&nbsp; &nbsp;$_SESSION['login_user'];} else {&nbsp; &nbsp;header("location:index.php");}

DIEA

试试这个:if(empty($SESSION['login_user'])){&nbsp; session_start(); //Start session and put session value}else{&nbsp; //Redirect here}

炎炎设计

上面的答案可以解决问题,但您还需要检查会话。如果会话正在运行,并且您进入session_start()存在该网页的网页,则会收到错误消息。请使用以下代码。if (session_status() == PHP_SESSION_NONE) {&nbsp; &nbsp; session_start();}

潇湘沐

试试这个$user = $_SESSION['login_user'];if(isset($user) && $user != ''){&nbsp; &nbsp;$query = "SELECT * from adminlogin where email = '$user'";&nbsp;&nbsp; &nbsp;$ses_sql = mysqli_query($conn, $query);&nbsp;&nbsp; &nbsp;$row = mysqli_fetch_assoc($ses_sql);&nbsp;&nbsp; &nbsp;$login_session = $row['nom'];&nbsp; &nbsp;$login_session2 = $row['id'];&nbsp; &nbsp;$login_session3 = $row['motdepasse'];}else{&nbsp; &nbsp;header("location:index.php");}
打开App,查看更多内容
随时随地看视频慕课网APP