PHP学习笔记_SESSION
2013-05-28 09:57
525 查看
<?php // page1.php /*create a new session if not find the SID session_unset() clear the variables of the session. session_destroy() destroy the session completely . session_encode() encode all variables in session then it can be stored in DataBase . or so have the session_decode() mehtod */ session_start(); echo 'Welcome to page #1 your session ID:'.session.id(); $_SESSION['favcolor'] = 'green'; $_SESSION['animal'] = 'cat'; //unset($_SESSTION['favcolor']); $_SESSION['time'] = time(); // Works if session cookie was accepted echo '<br /><a href="page2.php">page 2</a>'; // Or maybe pass along the session id, if needed echo '<br /><a href="page2.php?' . SID . '">page 2</a>'; ?> After viewing page1.php, the second page page2.php will magically contain the session data. Read the session reference for information on propagating session ids as it, for example, explains what the constant SID is all about. 例子 2. A session example: page2.php <?php // page2.php session_start(); echo 'Welcome to page #2<br />'; echo $_SESSION['favcolor']; // green echo $_SESSION['animal']; // cat echo date('Y m d H:i:s', $_SESSION['time']); // You may want to use SID here, like we did in page1.php echo '<br /><a href="page1.php">page 1</a>'; ?> 1. mysql_real_escape_string() 例子 <?php $item = "Zak's and Derick's Laptop"; $escaped_item = mysql_real_escape_string($item); printf ("Escaped string: %s\n", $escaped_item); //tells how many rows are in this result set and if it equals one, does that mean the user's authenticated. if(mysql_num_rows($result)==1) ?> \ goes in front of the semicolon or single quote It's pretty compelling 以上例子将产生如下输出: Escaped string: Zak\'s and Derick\'s Laptop sanity check 完整性 检查 isset -- 检测变量是否设置 bool isset ( mixed var [, mixed var [, ...]]) 如果 var 存在则返回 TRUE,否则返回 FALSE。 如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节("\0")并不等同于 PHP 的 NULL 常数。 警告: isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置 显示出最近浏览的文章 mysqli : create table articles <?php $db = new mysqli("localhost","webuser","secret","corporate"); // '?' is a placeholder for the params $stmt = $db->prepare("select id,title,content from articles where id = ?"); $stmt->bind_param('i',$_GET['id']); //get data from the form : <form method="post" action=" //<?php echo $_SERVER['PHP_SELF'];>"> $stmt->execute(); $stmt->store_result(); if($stmt->num_rows == 1) { //将返回的结果集分别绑定到php变量中 $stmt->bind_result($id,$title,$content); } //添加文章标题并连接到列表 $articleLink = "<a href='article.php?id={$id}'>{$title}</a>" if(! in_array($articleLink,$_SESSION['articles'])) $_SESSION['article'][] = $articleLink; //显示文章 echo "<p>$title</p><p>$content</p>"; //显示请求文章列表 echo "<p>Recently Viewed Articles</p>"; echo "<ul>"; foreach($_SESSION['articles'] as $doc) { eho "<li>$doc</li>"; } echo "</ul>"; ?> in_array() -- 检查数组中是否存在某个值 <?php $os = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $os)) { print "Got Irix"; } if (in_array ("mac", $os)) { print "Got mac"; } ?> mysql_fetch_assoc($result) //fetch associative array 获取关联数组
相关文章推荐
- 【小白笔记】PHP学习之路 (31) --cookie和session
- php学习笔记(二十七)读取session和删除session
- php session学习笔记(实例代码)
- PHP学习笔记 11 - Session
- php学习笔记--session会话
- php学习笔记-会话控制简单介绍session和cookie(一)
- php 学习笔记 -- Cookie 和 Session(六)
- php session学习笔记
- PHP学习笔记:使用session来存储用户的登录信息
- PHP学习笔记-Session
- php学习笔记--session的销毁
- PHP学习笔记【27】--服务端存储技术Session
- PHP学习笔记——表单数据获取,Session,Cookie
- PHP学习笔记:删除与销毁session
- PHP学习笔记【27】--服务端存储技术Session
- php学习笔记(二十六)php中session的初步使用(基于cookie的)
- 我的php学习笔记(二十九)PHP5中Cookie与 Session详解
- PHP学习笔记九、cookie与session
- php学习笔记(二十七)php中session的使用(基于url的)
- php学习笔记(二十五)认识session