用户注册后,需要用户的登陆,退出
需要知识点:cookie & session
看自己的注册资料,即用户表的自己的信息
容易,连上MySQL,查询数据,地址栏传参,传user_id
根据user_id 查询用户信息
但是存在问题,其他人也可以访问
<?php $user_id = $_GET['user_id'] + 0; $conn = mysql_connect('localhost','root',''); $sql = 'use boolshop'; mysql_query($sql); $sql = 'set names gbk'; mysql_query($sql); $sql = 'select * from user where user_id = '.$user_id; $rs = mysql_query($sql); print_r(mysql_fetch_assoc($rs)); /*Array ( [user_id] => 6 [username] => sadsad [email] => [email protected] [passwd] => b447c27a00e3a348881b0030177000cd [regtime] => 1397216877 [lastlogin] => 0 ) */ ?> |
那怎么才能控制,只能自己看到自己的信息呢?
02.php
<?php setcookie('username','zhangsan'); echo "<a href='03.php'>03.php</a>" ?> |
Request URL:http://localhost/execise/20140411/02.php
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:username=zhangsan; ECS[visit_times]=2
Host:localhost
User-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Response Headersview source
Connection:Keep-Alive
Content-Length:27
Content-Type:text/html
Date:Fri, 11 Apr 2014 12:18:25 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.8(Win32) PHP/5.2.6
Set-Cookie:username=zhangsan
X-Powered-By:PHP/5.2.6
03.php
<?php print_r($_COOKIE);//Array ( [username] => zhangsan ) echo "你是".$_COOKIE['username'];//你是zhangsan ?> |
RequestURL:http://localhost/execise/20140411/03.php
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:username=zhangsan; ECS[visit_times]=2
Host:localhost
Referer:http://localhost/execise/20140411/02.php
User-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Response Headersview source
Connection:Keep-Alive
Content-Length:108
Content-Type:text/html
Date:Fri, 11 Apr 2014 12:23:21 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.8(Win32) PHP/5.2.6
X-Powered-By:PHP/5.2.6
是如何读取到cookie的?
九大超全局变量,$_COOKIE读取