【发布时间】:2018-10-15 17:53:40
【问题描述】:
我在使用 PHP 启动会话时遇到问题。通过环顾四周,我写了一些应该可以工作但没有用的代码。你能帮帮我吗,因为我不知道这里出了什么问题。这是我的 logging.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
我开始这个会话是因为我希望能够看到哪个用户在网站上执行某些功能。但是我收到此错误消息: 注意:未定义的索引: 我知道错误是什么意思,但我不知道如何解决它,有什么帮助吗?
【问题讨论】:
-
echo "Welcome" .$_SESSION['username']; -
您需要在每个页面上致电
session_start。现在,在你的 login.php 中,你正试图将项目塞入一个你甚至还没有开始的会话中。 -
您应该定义要显示的会话。试试
$_SESSION['username'] -
每次在新代码中使用the
mysql_数据库扩展this happens 时,它都已被弃用,并且已经存在多年,并且在 PHP7.0+ 中永远消失了。如果您只是学习 PHP,请花精力学习PDO或mysqli数据库扩展和预处理语句。 Start here -
小点
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 andphpMyAdmin`只是一个工具
标签: php html session undefined-index