我的 session 有问题;我理论上知道,但我遇到了如何使用 session ,转移用户名到其他页面并添加 logout 以清除当前记录的帐户信息。 由于我不知道 session 的用法,所以我对其进行了评论。
这是checklogin.php
<?php
session_start();
include("database.php");
// username and password sent from form
$name=$_POST['email'];
$password=$_POST['pwd'];
//select data from database
$sql="SELECT * FROM $tbl_name WHERE usermail='$name' && userpasswd='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if( $count == 1) {
// Register $myusername, $mypassword and redirect to file "search.php"
//session_register("$name");
//session_register("$password");
//$_SESSION['name']= $name;
header("location:jcte/index.php");
} else {
$msg = "Wrong Username or Password. Please retry";
header("location:ErrorPage.html");
}
?>
最佳答案
行后
$result=mysql_query($sql);
添加
if ($data = mysql_fetch_array($result)) {
$_SESSION['user'] = $data['usermail'];
}
现在 session 已创建。在 jcte/index.php 页面中将此 session 调用为:
<?php
session_start();
echo "welcome $_SESSION['user']";
?>
在 logout.php 页面中取消设置 session :
<?php
session_start();
unset($_SESSION['user']);
?>
关于php - 如何在网页中使用session?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838973/