如何在网页内使用会话? [英] How to use session inside webpages?
本文介绍了如何在网页内使用会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到会话时遇到了麻烦;从理论上我知道它,但是我遇到了如何使用会话,转移用户名到其他页面并添加注销以便清除当前已记录的日志帐户信息. 由于我不知道会话使用情况,因此我对此进行了评论.
I am trouble with session; I know it theoretically but I encountered with it how to use session , transferring username to other page and adding logout in order to clear the current logged account information. Since I have no idea of session usage I commented it.
这是checklogin.php
This is the 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'];
}
现在已创建会话.在 jcte/index.php 页面中以以下方式调用此会话:
Now session created.Call this session in jcte/index.php page as:
<?php
session_start();
echo "welcome $_SESSION['user']";
?>
将logout.php页面中的会话设置为:
Unset the session in logout.php page as:
<?php
session_start();
unset($_SESSION['user']);
?>
这篇关于如何在网页内使用会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文