3 回答

TA貢獻1744條經驗 獲得超4個贊
如前所述,您必須在要使用會話的每個文件的開頭啟動會話。
如果要設置會話,請使用以下命令:
session_start();
$_SESSION[NAME] = VALUE;
并在檢查會話是否設置后重定向,您可以這樣做:
session_start();
if (isset($_SESSION[NAME]){
header(‘Location: index.php‘;
}

TA貢獻1863條經驗 獲得超2個贊
試一試。
// ensure to add session_start at the beginning of
// all scripts that require use of $_SESSION[]
session_start();
if($sql->rowCount()){
$entrou = $_SESSION['entrou'];
header('location: panel.php');
}
if(isset($_SESSION['entrou'])){
unset($_SESSION['entrou']);
header('location: index.php');
}

TA貢獻1784條經驗 獲得超2個贊
在這里試試這個
<?php
session_start();
...
if($sql->RowCount()>0){
$_SESSION['entrou'] = true;
header('location: panel.php');
}
else {
header('location: index.php'); // "no user in the db!"
}
...
if (!isset($_SESSION['entrou']) {
header('location: index.php');
}
?>
但當然,如果你想檢查用戶是否存在于數據庫中,你應該做這樣的事情
<?php
$data = $sql->query('SELECT * WHERE username=$_SESSION["username"]') // where "username" is the username column
if ($data == "") {
header('location: index.php'); // user is non-existent
}
else {
header('location: panel.php'); // the user is in the database
}
// note this only applies if you're using SQLite
- 3 回答
- 0 關注
- 206 瀏覽
添加回答
舉報