search
Homephp教程php手册长沙发上的对话(二)

 

第一个session
  
  
  关于演示session是如何工作的标准例子之一就是点击计数的应用 -- 这是一个简单的基于session的计数器,在你第一次访问一个web页面时初始化一个变量,每一次当你重新装入这个页面时增加它的计数。代码如下:
  
  php
  
  //
初始化一个session session_start();
  
  //
注册一个session变量 session_register('counter');
  
  ?>
  
  
  在PHP4中每一个session都通过调用session_start()函数开始,这个函数检查一个session是否存在,如果不存在则创建一个新的。接着,用session_register()函数来注册一个变量,它将生存在整个session -- 在上面的例子中,变量名为"counter",并且没有给它赋任何值。
  
  
  现在,让我们在上面的例子中加几行代码,点击计数将开始工作了:
  
  php
  
  //
初始化一个session session_start();
  
  //
注册一个session变量 session_register('counter');
  
  //
增加计数器 $counter++;echo("You have visited this page $counter times! Don't you have anything else to do, you bum?!");
  
  ?>

  
  
  试一试它!当你每一次重装这个页面时,计数器的值都在增加,这个说明了在session中变量是如何被保存的。
  
  
  为什么为发会生这样的情况呢?唔,每次一个session被创建,一个sessioncookie[叫做PHPSESSID]会在客户系统中被创建并且被赋了一个随机数;同时,在服务器端一个相似的入口被创建,它包含着在session中注册过的变量。在客户与服务器之间的通讯就通过这个有相同命名的session的标识号(id)来实现,这样就可以在整个session期间保存不同的变量。
  
  
  让我们再看一下复杂一点的吧?看这个例子,它演示了一个使用session变量的计时器,它将告诉你在重新装入页面时过了多长的时间。
  
  php
  
  session_start();
  
  //session
变量用于保存计数器session_register('counter');
  
  //session
变量用于保存最后装入的时间值
  
  //
这个值被保存以便比较两次的不同session_register('timeAtLastLoad');
  
  //
当前时间$timeNow = time();
  
  //
增加计数$counter++;
  
  //
计算两次时间间隔$timeLapsed = $timeNow - $timeAtLastLoad;
  
  //
显示信息if($counter > 1)
  {
  echo "It's been $timeLapsed seconds since you last viewed
  this page.
";
  }
  else
  {
  echo "First time here? Reload this page to see how the
  session works!/b>";
  
  }
  
  $timeAtLastLoad = $timeNow;
  
  ?>
  
  
  
  



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools