PHP的会话掌控解析
<h2 style="color: black; text-align: left; margin-bottom: 10px;">会话<span style="color: black;">掌控</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span> HTTP 是<span style="color: black;">没</span>状态的协议,<span style="color: black;">无</span>办法记录多个事务请求间的状态。即<span style="color: black;">拜访</span>一个页面请求后再请求另一个页面时<span style="color: black;">没</span>法判断两次请求来自同一下用户。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">会话原理</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP中实现会话的原理是为用户分配一个<span style="color: black;">独一</span>的加密ID,并<span style="color: black;">保留</span>在用户客户端,并在<span style="color: black;">全部</span>会话时间周期内有效。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">通常</span><span style="color: black;">状况</span>下<span style="color: black;">运用</span>cookie来进行<span style="color: black;">保留</span>,但<span style="color: black;">亦</span>可以<span style="color: black;">经过</span>url传递。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用户<span style="color: black;">经过</span>携带会话ID到服务器获取内容。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">COOKIE</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cookie是客户端存储数据的手段,并在请求服务器时自动携带cookie数据。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">设置</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP中<span style="color: black;">运用</span> setcookie() 函数用户客户端cookie数据。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie ( string $name [, string $value = "" [, int $expires = 0 [, string $path = "" [, string $domain = "" [, bool $secure = FALSE [, bool $httponly = FALSE ]]]]]] ) : bool</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参数说明如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参数说明nameCookie的名<span style="color: black;">叫作</span>valuecookie的价值。该值存储在客户端计算机上; 不要存储<span style="color: black;">敏锐</span>信息expiresCookie过期的时间。这是一个Unix时间戳,<span style="color: black;">能够</span><span style="color: black;">运用</span><a style="color: black;">time()</a>函数加上<span style="color: black;">期盼</span>它到期之前的秒数来设置它。path服务器上<span style="color: black;">能够</span><span style="color: black;">运用</span>cookie的路径。<span style="color: black;">倘若</span>设置为“/”,则cookie将在<span style="color: black;">全部</span>范围内可用domaincookie可用的域。(例如“www.houdunren.com”)将使cookie可用于该子域及其所有其他子域(即<a style="color: black;"><span style="color: black;">http://</span><span style="color: black;">w2.www.houdunren.com</span></a>)。要使cookie可用于<span style="color: black;">全部</span>域(<span style="color: black;">包含</span>其所有子域),只需将值设置为域名(在本例中为“houdunren.com”)secure<span style="color: black;">暗示</span>cookie应仅<span style="color: black;">经过</span>客户端的安全HTTPS连接传输httponly当TRUEcookie只能<span style="color: black;">经过</span>HTTP协议<span style="color: black;">拜访</span>时。这<span style="color: black;">寓意</span>着脚本语言(例如JavaScript)<span style="color: black;">没</span>法<span style="color: black;">拜访</span>cookie。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">读取</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>cookie<span style="color: black;">保留</span>在客户端并在请求时提交到后台服务器,<span style="color: black;">因此</span><span style="color: black;">能够</span>在前台与后台<span style="color: black;">拜访</span>到cookie。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前台<span style="color: black;">运用</span> document.cookie 获取cookie数据</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">document.cookie</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后台<span style="color: black;">运用</span>超全避数组$_COOKIE读取</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">print_r($_COOKIE);</div>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">运用</span></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置会话cookie,当关闭浏览器时自动删除</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,houdunren.com);</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置七天内有效的cookie</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,houdunren.com,time()+60*60*24*7);</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">只<span style="color: black;">准许</span>cookie在 /app <span style="color: black;">拜访</span>路径中有效</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,houdunren.com,0,/app)</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置<span style="color: black;">拜访</span>域名</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,hdcms.com,0,/,php.test)</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置只<span style="color: black;">准许</span>https<span style="color: black;">拜访</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,hdcms.com,0,/,,true);</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不<span style="color: black;">准许</span>javascript操作cookie</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(web,hdcms.com,0,,,false,false,true);</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将cookie的到期时间设置为过去时间,就<span style="color: black;">能够</span>删除cookie。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">setcookie(name,,1);</div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">SESSION</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">session是服务器会话状态,可用于记录<span style="color: black;">拜访</span>用户后台会话数据。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">基本<span style="color: black;">运用</span></h3>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">开启会话</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">必要</span>要开启 SESSION 才<span style="color: black;">能够</span><span style="color: black;">运用</span>,有两种<span style="color: black;">办法</span><span style="color: black;">能够</span>开启会话。</p><span style="color: black;">运用</span>脚本开启会话,要<span style="color: black;">保准</span>在<span style="color: black;">运用</span>会话的所有脚本中执行。<div style="color: black; text-align: left; margin-bottom: 10px;">session_start();</div><span style="color: black;">经过</span>修改php.ini配置项 session.auto_start,自动开启会话(不<span style="color: black;">意见</span><span style="color: black;">运用</span>)<h3 style="color: black; text-align: left; margin-bottom: 10px;">会话变量</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>超全局数组 $_SESSION 来<span style="color: black;">运用</span>会话变量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置变量</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">$_SESSION=houdunren.com;</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">删除变量</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">unset($_</div>
系统提示我验证码错误1500次 \~゛, 外链发布社区 http://www.fok120.com/
页:
[1]