我遇到了在php文件中显示我的会话的问题。我可以让会议显示,但它显示不正确 - 它只显示字母'S'。查询数据库和会话
会话是在用户登录到我的系统时创建的,它们包含其唯一ID和全名。
我的登录php如下。只有'full_name'和'id'会话我有问题,因为我必须自己将它们编码到脚本中。 我有探头的地方
<?php
ob_start();
include ("includes/dbConfig.php");
$tbl_name="users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$un", "$pwd")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$encrypted_password=md5($password);
$username = mysql_real_escape_string($username);
$encrypted_password = mysql_real_escape_string($encrypted_password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$encrypted_password'";
$result=mysql_query($sql);
//Get User Info
$userinfo="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($userinfo);
$full_name= $userinfo['full_name'];
$id= $userinfo['id'];
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
// Register $username, $password and redirect to file "console.php"
session_start();
$_SESSION['username'] = $username;
$_SESSION['full_name'] = $full_name;
$_SESSION['id'] = $id; // store session data
header("Location: console.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
犯了一个错误我已经测试了我的站点内的会议,并横跨确定一些虚拟的信息传递。我认为它是从数据库中获取信息的问题。
我知道md5并不是最安全的密码加密方式,但是对于我所需要的就可以了。
谢谢先进。
这是我的页面顶部的代码。
<?php
include ("includes/dbConfig.php");
session_start();
if(!$_SESSION['username'])
if(!$_SESSION['full_name'])
if(!$_SESSION['id']) {
// user not logged in redirect
header("Location: index.php");
}
?>
这是我用来打印会话数据的代码。
<?php echo "" . $_SESSION["username"]; ?>
<?php echo "" . $_SESSION["full_name"]; ?>
<?php echo "" . $_SESSION["id"]; ?>
确实console.php具有'在session_start();'中呢?如果没有,那是你的问题。 – Popnoodles
是的,它的确如此。 '用户名'会话通过确定,因为我可以让我的用户名打印。它只是其他两个,当我打印他们的字母'S'刚刚显示。 – user1948851
你能提供打印它们的代码吗? – Popnoodles