PHP 会话无法开始
PHP Session won't start
我在 PHP 中开始会话时遇到问题。通过环顾四周,我写了一些应该可以工作但没有工作的代码。你能帮帮我吗,因为我不知道这里出了什么问题。这是我的loging.php页面
<?php
$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
$errore = "Login info are wrong!`enter code here`";
mysql_connect($host,$user,$password);
mysql_select_db($db);
if(isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from utenti where username = '".$username."' AND Password = '".$password."' limit 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)==1){
$_SESSION['username'] = $username;
header("location:index.php");
}
else{
echo "" .$errore;
}`enter code here`
}
?>
我的数据库与 phpmyamin 上的用户以及它正在运行的登录有关。问题是当我加载 index.php 页面时。
<?php
session_start();
echo "Welcome" .$_SESSION[''];
?>
<html>
all the html code
我开始这个会话是因为我希望能够看到哪个用户在网站上执行 certian 功能。但是我收到此错误消息:
注意:未定义索引:
我知道这个错误是什么意思,但我不知道如何解决,有帮助吗?
在每个要使用会话的页面中使用 session_start()
,并且在 loging.php
页面中设置 $_SESSION['username']
,因此您需要更改
echo "Welcome" .$_SESSION[''];
和
echo "Welcome" .$_SESSION['username'];
这样,您就可以得到您在loging.php
页面
设置的index.php
中username
的session
所以,首先,我看到您使用的是 mysql_connect
,这是一个已弃用的函数,因为它根本不安全,为了文档,它已被 mysqli_connect
http://php.net/manual/en/book.mysqli.php 取代。为了更好的安全性,并防止 sql 注入,您应该使用 PDO,或准备好的语句。不过在这个例子中,我一直坚持使用 mysqli
,因为它不太容易学习。
其次,$_SESSION
仅在您首先使用 session_start()
初始化会话时才有效。这必须在您希望从中读取或写入会话数据的每个 页面上完成。
<?php
//Since this page writes to a session, initialise it here
session_start();
//The values to connect to the database with
$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
//Create a new mysqli connection to the database
$conn = mysqli_connect($host, $user, $password, $db);
//This is the error message that's displayed on unsuccessful login
$error = "Login info are wrong!`enter code here`";
//This is the error message if the username is not specified
$errorNoUsername = "You have not specified a username";
/**
* Now that we're using mysqli_connect(), we don't need this code.
* mysql_connect($host,$user,$password);
* mysql_select_db($db);
**/
//See if the user has submitted the form with the username parameter
if(isset($_POST['username'])){
//If they have, shortname the variable for username and password
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
//Build your select query. In production, you should use PDO or Prepared Statements to protect against injection
//I've removed your LIMIT 1 from the query, because I see you're checking for a distinct match later on with mysqli_num_rows==1
$sql = "SELECT * FROM utenti WHERE username='".$userUsername."' AND Password = '".$userPassword."'";
//run the query on the connection created earlier
$result = mysqli_query($conn, $sql);
//Check if there's a distinct match
if(mysqli_num_rows($result)==1){
//There is, good, initialise session with the user data
$_SESSION['username'] = $userUsername;
//Reload to your index.php page
header("location:index.php");
} else {
//Display the error message
echo $error;
}
} else {
echo $errorNoUsername;
}
?>
现在我们已经完成了,假设登录成功,我们将用户重定向回 index.php
,因为我们正在读取会话数据,我们需要再次初始化会话,使用 session_start();
,您已经完成了,但是您的密钥 $_SESSION['']
不存在,因此出现错误。在这里,我更正了。
<?php
session_start();
echo "Welcome, " . $_SESSION['username']; //Added keyname
?>
<html>
all the html code
</html>
我在 PHP 中开始会话时遇到问题。通过环顾四周,我写了一些应该可以工作但没有工作的代码。你能帮帮我吗,因为我不知道这里出了什么问题。这是我的loging.php页面
<?php
$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
$errore = "Login info are wrong!`enter code here`";
mysql_connect($host,$user,$password);
mysql_select_db($db);
if(isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from utenti where username = '".$username."' AND Password = '".$password."' limit 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)==1){
$_SESSION['username'] = $username;
header("location:index.php");
}
else{
echo "" .$errore;
}`enter code here`
}
?>
我的数据库与 phpmyamin 上的用户以及它正在运行的登录有关。问题是当我加载 index.php 页面时。
<?php
session_start();
echo "Welcome" .$_SESSION[''];
?>
<html>
all the html code
我开始这个会话是因为我希望能够看到哪个用户在网站上执行 certian 功能。但是我收到此错误消息: 注意:未定义索引: 我知道这个错误是什么意思,但我不知道如何解决,有帮助吗?
在每个要使用会话的页面中使用 session_start()
,并且在 loging.php
页面中设置 $_SESSION['username']
,因此您需要更改
echo "Welcome" .$_SESSION[''];
和
echo "Welcome" .$_SESSION['username'];
这样,您就可以得到您在loging.php
页面
index.php
中username
的session
所以,首先,我看到您使用的是 mysql_connect
,这是一个已弃用的函数,因为它根本不安全,为了文档,它已被 mysqli_connect
http://php.net/manual/en/book.mysqli.php 取代。为了更好的安全性,并防止 sql 注入,您应该使用 PDO,或准备好的语句。不过在这个例子中,我一直坚持使用 mysqli
,因为它不太容易学习。
其次,$_SESSION
仅在您首先使用 session_start()
初始化会话时才有效。这必须在您希望从中读取或写入会话数据的每个 页面上完成。
<?php
//Since this page writes to a session, initialise it here
session_start();
//The values to connect to the database with
$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
//Create a new mysqli connection to the database
$conn = mysqli_connect($host, $user, $password, $db);
//This is the error message that's displayed on unsuccessful login
$error = "Login info are wrong!`enter code here`";
//This is the error message if the username is not specified
$errorNoUsername = "You have not specified a username";
/**
* Now that we're using mysqli_connect(), we don't need this code.
* mysql_connect($host,$user,$password);
* mysql_select_db($db);
**/
//See if the user has submitted the form with the username parameter
if(isset($_POST['username'])){
//If they have, shortname the variable for username and password
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
//Build your select query. In production, you should use PDO or Prepared Statements to protect against injection
//I've removed your LIMIT 1 from the query, because I see you're checking for a distinct match later on with mysqli_num_rows==1
$sql = "SELECT * FROM utenti WHERE username='".$userUsername."' AND Password = '".$userPassword."'";
//run the query on the connection created earlier
$result = mysqli_query($conn, $sql);
//Check if there's a distinct match
if(mysqli_num_rows($result)==1){
//There is, good, initialise session with the user data
$_SESSION['username'] = $userUsername;
//Reload to your index.php page
header("location:index.php");
} else {
//Display the error message
echo $error;
}
} else {
echo $errorNoUsername;
}
?>
现在我们已经完成了,假设登录成功,我们将用户重定向回 index.php
,因为我们正在读取会话数据,我们需要再次初始化会话,使用 session_start();
,您已经完成了,但是您的密钥 $_SESSION['']
不存在,因此出现错误。在这里,我更正了。
<?php
session_start();
echo "Welcome, " . $_SESSION['username']; //Added keyname
?>
<html>
all the html code
</html>