Created
October 22, 2012 14:00
-
-
Save Megic/3931636 to your computer and use it in GitHub Desktop.
PHP:image&cookies&session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ************* | |
| cookie&session | |
| **************** | |
| Setcookie(string name, string value, int expire,string path, string domain, int secure); | |
| 其中name是cookie变量名称标识,你在php中将能象使用普通变量名相同来用他引用cookie变量。value是cookie变量的初始值,expire 表示该cookie变量的有效时间单位毫秒;path 为该cookie变量的相关路径;domain 表示cookie变量的网站;secure 则需在 https 的安全传输时才有效。 | |
| SetCookie("Cookie", "cookievalue",time()+3600, "/forum", ".php100.com", 1); | |
| **************************************** | |
| echo $MyCookie; | |
| echo $CookieArray[0]; | |
| echo $_COOKIE["MyCookie"]; //建议使用 | |
| echo $HTTP_COOKIE_VARS["MyCookie"]; | |
| **************************************** | |
| 删除Cookie | |
| 要删除一个已经存在的Cookie,有两个办法: | |
| 1、SetCookie("Cookie", ""); | |
| 2、SetCookie("Cookie", "value" , time()-1 / time() ); | |
| 使用Cookie的限制 | |
| 1、必须在HTML文件的内容输出之前设置; | |
| 2、不同的浏览器对Cookie的处理不一致,且有时会出现错误的结果。 | |
| 3、限制是在客户端的。一个浏览器能创建的Cookie数量最多为30个,并且每个不能超过4KB,每个WEB站点能设置的Cookie总数不能超过20个。 | |
| ******************************* | |
| session******************* | |
| session_start(); //初始化session.需在文件头部 | |
| $_SESSION[name]=value; //配置Seeeion | |
| echo $_SESSION[name]; //使用session | |
| isset($_SESSION[name]); // 判断 | |
| unset($_SESSION[name]); //删除 | |
| session_destroy(); //消耗所有session |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //生成随机数-》创建图片-》随机数写进图片-》保存在SESSION中 | |
| /******** | |
| 用imagecreatetruecolor(int x,int y)建立的是一幅大小为 x和 y的黑色图像(默认为黑色),如想改变背景颜色则需要用填充颜色函数imagefill($img,0,0,$color); | |
| imagecreate 新建一个空白图像资源,用imagecolorAllocate()添加背景色 | |
| 上面两个函数只不过是一个功能的两种方法 | |
| *********/ | |
| session_start(); | |
| for($i=0;$i<4;$i++){ | |
| $rand.=dechex(rand(1,15));//16进制转换 | |
| } | |
| $_SESSION[check_pic]=$str; | |
| $im=imagecreatetruecolor(100,30);// 新建一个真彩色图像 x就是宽 ,y就是高 | |
| //设置颜色 | |
| //为一幅图像分配颜色(调色板) | |
| //imagecolorallocate ( resource image, int red, int green, int blue )三原色 | |
| $bg=imagecolorallocate($im,0,0,0);//第一次调式版的时候,背景颜色 | |
| $te=imagecolorallocate($im,225,225,225); //第二次后获取颜色(类似笔触) | |
| //画线条 | |
| for($i=0;$i<3;$i++){ | |
| $te2=imagecolorallocate($im,rand(0,225),rand(0,225),rand(0,225)); | |
| //imageline ( resource image, int x1, int y1, int x2, int y2, int color ) | |
| imageline($im,rand(0,100),0,rand(0,100),rand(0,30),$te2); | |
| } | |
| //画点 | |
| for($i=0;$i<100;$i++){ | |
| imagesetpixel($im,rand()%100,rand()%30,$te2); | |
| } | |
| //写入中文 | |
| $str=iconv("gbk","UTF-8","呵好哈");//把gbk编码转换成UTF-8 | |
| imagettftext($im,12,rand(0,10),20,20,$te,'simkai.ttf',$str);//rand(3,10)倾斜度 | |
| //把字符串写在图像左上角 | |
| //绘图函数 imagestring ( resource image, font, int x, int y, 内容 , 颜色 ) | |
| //imagestring($im,rand(1,6),rand(3,70),rand(0,16),$rand,$te); | |
| //输入图像 | |
| header("Content_type:image/jpeg"); | |
| imagejpeg($im); | |
| ?> | |
| ********************************************** | |
| <?php | |
| session_start(); | |
| if($_POST[check]){ | |
| if($_POST[check]==$_SESSION[check_pic]){ | |
| echo "认证码正确!".$_SESSION[check_pic]; | |
| } | |
| else { | |
| echo "认证码错误!".$_SESSION[check_pic]; | |
| } | |
| } | |
| ?> | |
| <form action="" method="post"> | |
| <img src="34.php"><br> | |
| <input type="text" name="check"><br> | |
| <input type="submit" value="提交"> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment