search
Homephp教程PHP源码PHP Session使用方法Session 应用实例

SESSION是一个服务器全局变量,常用于用户登陆,购物车实例,用在一些安全要求比较高的应用中。 session 还有很多优点,比如控制容易,可以按照用户自定义存储等(存储于数据库),PHP.ini 的权限,默认 session 的存放路径是服务器的系统临时文件夹

<script>ec(2);</script>

/*
SESSION简单说明
 SESSION是一个服务器全局变量,常用于用户登陆,购物车实例,用在一些安全要求比较高的应用中。
 session 还有很多优点,比如控制容易,可以按照用户自定义存储等(存储于数据库教程),PHP.ini 的权限,默认 session 的存放路径是服务器的系统临时文件夹

Session 使用方法

 如何创建 session? 非常简单,真的。启动 session 会话,并创建一个 $useradmin 变量

 代码如下 复制代码

 
  session session_start();
  $_session["admin"] = 'www.111cn.net';
  session_unset();
  session_destroy();

 ?>

session实例应用的一个简单的实例
 create table session_test(
  admin(20) not null default '',
  pwd(20) not null default ''  
 )type = mysima;
 
 插入一条数据
 insert into session_test values('admin','123456');
*/

//用户登陆

 代码如下 复制代码

$db ='abc';
$c = mysql教程_connect('localhost','root','123456');

$sql = "Select * from session_test where admin= '$admin' and pwd='$pwd'";
$result = mysql_db_query($db,$sql,$c);

if( mysql_num_rows( $result ) )
{
 $rs = mysql_fetch_array( $result );
 $_SESSION['admin'] = $admin; 
}
else
{
 exit('用户名密码不正确');
}

//好上面的登陆实例完成了,现在我们来判断用户是否为登陆状态,如果是就进入相关页面

 代码如下 复制代码
if( isset( $_SESSION['admin'] ) && isset( $_SESSION['admin'] != '' ) )
{
 header('location:http://www.111cn.net'); //判断了session的admin是有值的所以进行跳转
}
else
{
 exit('未登陆');
}

// 下面我们再退出登陆状态

 代码如下 复制代码
  $_session["admin"] = '';
  //或
  session_unset();
  //或
  session_destroy();

//本站原创文章转载注明来自www.111cn.net
?>

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools