提问人:TheNoobUser 提问时间:10/15/2018 更新时间:10/15/2018 访问量:4874
PHP 会话无法启动 [已关闭]
PHP Session won't start [closed]
问:
我在PHP中启动会话时遇到问题。通过环顾四周,我编写了一些应该可以工作的代码,但它没有。你能帮帮我吗,因为我不知道这里出了什么问题。这是我的登录.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上的用户以及它正在工作的登录名有关。问题是当我加载索引.php页面时。
<?php
session_start();
echo "Welcome" .$_SESSION[''];
?>
<html>
all the html code
我开始这个会话是因为我希望能够看到哪个用户在网站中执行某些功能。但是,我收到以下错误消息: 注意:未定义的索引: 我知道错误意味着什么,但我不知道如何解决它,有什么帮助吗?
答:
在要处理会话的每个页面中使用,并在页面中设置时使用,因此需要更改session_start()
$_SESSION['username']
loging.php
echo "Welcome" .$_SESSION[''];
跟
echo "Welcome" .$_SESSION['username'];
这样,您将能够获得您在页面中设置的会话username
index.php
loging.php
评论
session_start();
php
session_start()
session_start()
所以,首先,我看到你正在使用 ,这是一个已弃用的功能,因为它根本不安全,它被文档 http://php.net/manual/en/book.mysqli.php 所取代。为了获得更好的安全性,并防止 sql 注入,应使用 PDO 或预准备语句。不过,在这个例子中,我坚持使用,因为它的学习曲线较少。mysql_connect
mysqli_connect
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>
评论
$username
$userUsername
$_SESSION
评论
echo "Welcome" .$_SESSION['username'];
session_start
$_SESSION['username']
mysql_
数据库扩展时,都会发生这种情况,它被弃用并且已经存在多年,并且在 PHP7.0+ 中永远消失了。如果您只是在学习 PHP,请将精力花在学习 or 数据库扩展和准备好的语句上。从这里开始PDO
mysqli
phpMyAdmin is a tool written in PHP to make fiddling with you **MYSQL** database easier then having to use the command line. So MYSQL is a database and