我来自经典的ASP编程背景,男孩PHP真的令人沮丧。与PHP会话有什么关系?在经典ASP中你简单地说:
<% Session("Name") = "XYZ" %>
除非你杀掉它或它超时,否则该会话始终可用。使用PHP,我可以从一个页面到另一个页面使用会话,但是当我刷新页面时,我会丢失会话。这是我的代码:
页面:modules.php
// Start the session
session_start();
页面:index.php
include 'modules/modules.php';
$_SESSION['username'] = "MyName";
if (isset($_SESSION['username']) && !empty($_SESSION['username']) {
header('Location: main.php');
}
页面:main.php
include 'modules/modules.php';
echo "My username: ".$_SESSION['username'];
exit();
现在因为我给会话用户名一个默认值,它会重定向到main.php并显示用户名。但是,如果我刷新页面,它就会消失。我运行它来查看启动会话正下方的modules.php页面是否有任何错误:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
没有人被退回。但我无法弄清楚为什么我的PHP会话就会消失。我正在尝试创建一个用户将登录的登录页面,并且他/她的信息将被传送到每个页面,这样我就可以获得ID信息并确保他们已登录。所以有人可以告诉我我在做什么错?
我的模块页面:
// Start the session
session_start();
/* Database Connection Settings */
$_SESSION['servername'] = "localhost";
$_SESSION['mysql_username'] = "xxxxxxx";
$_SESSION['mysql_password'] = "xxxxxxx";
$_SESSION['dbname'] = "mydb";
//Turn on Error Report. True = On / False = Off
ErrorReporting(true);
//Display Error.
function ErrorReporting($ErrOn){
if ($ErrOn == true) {
//Show Error
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
}
}
function db_conn($servername, $mysql_username, $mysql_password, $dbname) {
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}
/**************************************
Close Database Connection Function.
***************************************/
function db_disconn() {
$conn = null;
}
/***************************************
Employee Login Check:
****************************************/
function CheckLogin($strUserName, $strPassword) {
if (isset($strUserName) && !empty($strUserName) && isset($strPassword) && !empty($strPassword)) {
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname, user_name, password FROM tbl_employees WHERE user_name='$strUserName' AND password='$strPassword' AND account_disabled='';";
$result = $conn->query($sql);
//Check and see if there are records avaiable.
if ($result->num_rows > 0) {
// output data of each row with a loop.
while($row = $result->fetch_assoc()) {
//Store the info into a session variable.
$_SESSION['eid'] = $row["id"];
$_SESSION['firstname'] = $row["firstname"];
$_SESSION['lastname'] = $row["lastname"];
return $_SESSION["eid"];
//break; //Stop the loop process.
}
} else {
//No records found prompt the user.
return "User name or Password was Incorrect! Please try again!";
}
db_disconn(); /*Close db*/
}
}
答案 0 :(得分:0)
你正在调用$ _SESSION,好像它是一个函数 - $_SESSION("username")
- 而它实际上是一个数组。
您应该使用$_SESSION["username"]
来获取变量值。
您应该收到致命错误:无法在写入上下文中使用函数返回值... 但是您可能在启用错误报告时遇到了问题并且您没有收到任何错误
正确的工作代码如下所示:
<?php
session_start();
if (isset($_SESSION["username"]) && !empty($_SESSION["username"])) {
echo "My username: ".$_SESSION["username"];
} else {
echo "Not set";
$_SESSION["username"] = "MyName";
}