SESSION函数_PHP教程

WBOY
Release: 2016-07-13 10:31:51
Original
729 people have browsed it

SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION;
然后就可以使用SESSION变量了,比如说要赋值就是:$_SESSION['item']="item1";要得到值就是$item1=$_SESSION['item'];,很简单吧。这里我们可能会使用到一些函数,比如说判断是不是某SESSION变量为空,可以这么写:empty($_SESSION['inum'])返回true or false。

下面综合一下前面所说的我们来看一个登陆程序,判断用户名密码是否正确。
登陆表单是这样:login.php




















Administrators Login
Username


Password





处理文件是这样

require_once('conn.php');
session_start();
$username=$_POST['username'];
$password=$_POST['password'];
$exec="select * from admin where username='".$username."'";
if($result=mysql_query($exec))
{
if($rs=mysql_fetch_object($result))
{
if($rs->password==$password)
{
$_SESSION['adminname']=$username;
header("location:index.php");
}
else
{
echo "";
}
}
else
{
echo "";
}
}
else
{
echo "";
}

?>

conn.php是这样:

$conn=mysql_connect ("127.0.0.1", "", "");
mysql_select_db("shop");
?>

由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.asp

session_start();
if($_SESSION['adminname']=='')
{
echo "";
}
?>

www.bkjia.com true http://www.bkjia.com/PHPjc/760822.html TechArticle SESSION的作用很多,最多用的就是站点内页面间变量传递。在页面开始我们要session_start();开启SESSION; 然后就可以使用SESSION变量了,比如说...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!