search

SESSION函数

Jun 13, 2016 am 09:37 AM
sessiontransfereffectInsidefunctionvariableexiststartIuseofsitepage

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 "<script>alert('Password Check Error!');location.href='login.php';</script>";
     }
   }
   else
   {
   echo "<script>alert('Username Check Error!');location.href='login.php';</script>";
   }
   }
 else
 {
 echo "<script>alert('Database Connection Error!');location.href='login.php';</script>";
 }

?>

conn.php是这样:

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

由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.asp
 
 session_start();
 if($_SESSION['adminname']=='')
 {
 echo "<script>alert('Please Login First');location.href='login.php';</script>";
 }
 ?>

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools