php

xiaoxiao2021-02-28  99

重点内容

1.http 超文本文件传输协议 2.规则,协议:由一些人规定遵循的规则,然后使用HTTP方式请求文件的必须遵循这个规则(协议)

2.cookie

<script type="text/javascript"> //Cookie 浏览器本地存储的一种方式 //LocalStroage WebSql SessionStroage //SessionStroage 和后台的 Session不是一个东西 //浏览器的Cookie每对最大4 到 5kb //所以Cookie一般不用于存储过多的文字 //浏览器最多能有50对 //Cookie过期时间:默认为浏览器关闭时 //设计过期时间 //如果设置expires的时间是当前时间点,之前的时间,则马上失效 //var dat = new Date(); //在当前时间的基础上加上一天时间的毫秒数 //dat.setTime(dat.getTime() + 24 * 3600 * 1000);// 北京时间 //document.cookie = "name=wang;expires=" + dat.toUTCString(); //document.cookie = "sex=boy;expires=" + dat.toUTCString(); //document.cookie = "hobby=eat;expires=" + dat.toUTCString(); //获取Cookie(name对应的值) //console.log(document.cookie); //参数1:cookie的key值 //参数2:cookie的value值 //参数2:过期时间(天) function setJSCookie(key ,value, day){ var dat = new Date(); dat.setTime(dat.getTime() + day * 24 * 3600 * 1000); document.cookie = key + "=" + value + ";expires=" + dat.toUTCString(); } setJSCookie("wang" ,"111111", 2);//添加cookie function getJSCookie(key){ var arr = document.cookie.split("; "); for(var i = 0; i < arr.length; i++){ var str = arr[i]; var theArr = str.split("="); if(theArr[0] == key){ console.log(theArr[1]); } } } getJSCookie("wang");//查看cookie function removeJSCookie(key){ //传key //value 随便来一个,然后设置过期时间(当前时间 - 1 毫秒) //document.cookie = "key=value;expires=" + 时间 var nowD = new Date(); nowD.setTime(nowD.getTime() - 1000); document.cookie = key + "=" + "xx;expires="+ nowD.toUTCString(); } removeJSCookie("wang"); </script>

2.封装cookie

"use static"; var obj = { "setJSCookie" : setJSCookie, "getJSCookie" : getJSCookie, "removeJSCookie" : removeJSCookie } //TODO:---Cookie //参数1:cookie的key值 //参数2:cookie的value值 //参数2:过期时间(天) function setJSCookie(key, value, day) { var dat = new Date(); dat.setTime(dat.getTime() + day * 24 * 3600 * 1000); document.cookie = key + "=" + value + ";expires=" + dat.toUTCString(); } function getJSCookie(key) { var arr = document.cookie.split("; "); for(var i = 0; i < arr.length; i++) { var str = arr[i]; var theArr = str.split("="); if(theArr[0] == key) { console.log(theArr[1]); } } } function removeJSCookie(key) { //传key //value 随便来一个,然后设置过期时间(当前时间 - 1 毫秒) //document.cookie = "key=value;expires=" + 时间 var nowD = new Date(); nowD.setTime(nowD.getTime() - 1000); document.cookie = key + "=" + "xx;expires=" + nowD.toUTCString(); } ---------- 测试 <script src="wqq.js" ></script> <script type="text/javascript"> obj.setJSCookie("name","lisi",2); </script>

3.php设置/读取cookie

<?php //setcookie(key,value,expire,path,domain,secure) $a = md5(111111); setcookie("password",$a,time()+3600*24,"/",localhost,0); //setcookie("name", "", time() - 3600); //取值 var_dump($_COOKIE['password']); ?>

4.session之我见

1.sesseon.php <?php /* 第一次运行 session_start(); 1.生成一个cookie 是 PHPSESSID 2.在本地生成一个临时文件,用来存储PHPSESSID数据 */ session_start(); $_SESSION["name"] = "张三丰"; ?> 2.getsession.php <?php header("Content-type: text/html; charset=utf-8"); /* * 1.先判断cookies 中有没有PHPSESSID * 有:根据PHPSESSID读取对应本地文件,并将文件中的内容读取出来,存入$_SSESION中 * 无:在cookies中写入一个PHPSESSID,同时生成一个对应PHPSESSID的文件,用来存储session数据 * * guid * */ session_start(); echo $_SESSION['name']; ?> guid function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8) .substr($charid, 8, 4) .substr($charid,12, 4) .substr($charid,16, 4) .substr($charid,20,12); return $uuid; } echo create_guid(); /*GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”, 其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。 例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。 ★GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。 ★世界上的任何两台计算机都不会生成重复的 GUID 值。 ★需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。 ★GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。 */ <?php header("content-type:text/html;charset=utf-8"); //php中的Session是PHP的东西,而SessionStorage是浏览器的东西 //php.ini 搜索 maxlife 修改session的生命周期 //1.开启session(一般放在require下,其他代码上面) session_start(); //2.确定key与value的值 $_SESSION["name"] = "英雄"; //echo $_SESSION["name"]; //3.unset删除(连key值一起被清除掉) //对于数组,清除掉当前位置,不会影响后面元素的下角标) //array_shift() 会让后面元素的下角标一次向前移动 //unset($_SESSION["name"]); //var_dump( $_SESSION["name"]); //4.销毁所有(慎用:删除服务器上所有的session) session_unset();//先清空 session_destroy();//在释放 ?>

5.验证码

05php_use_yzm.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <body> <form> <input type="text" id="enter_yzm"/> <input type="submit" value="提交验证" onclick="up()"/> </form> <img src="05php_yzm.php" id="yzm"/><a href="javaScript:reloadImage()">刷新</a> <script type="text/javascript"> function reloadImage(){ //重新赋予img标签的src值的时候, //如果url一致,就会从缓存中取出这个url对应的图片 //想办法让url不一致 var theImg = document.getElementById("yzm"); var da = new Date(); theImg.src = "05php_yzm.php?"+"timetemp=" + da.getTime(); console.log(theImg.src); } function up(){ var enter_yzm = document.getElementById("enter_yzm").value; $.ajax({ type: "POST", url: "05php_check_code.php", data:{ "theCode":enter_yzm }, success: function(msg){ alert(msg ); }, error:function(e){ console.log(e.statusText); } }); } </script> </body> </html> ----------------------------------------- 05php_yzm.php <?php header("Content-Type: image/png"); // for($i = 0; $i < 4; $i++){ // $rand.=dechex(rand(1,15)); // } $rand = ""; // 1-9 a-z A-Z // 随机0-2数字 for ($i = 0; $i < 4; $i++){ $p = rand(0, 2); switch ($p){ case 0: $num = rand(49, 57); // 1-9随机取一个 $rand = $rand.chr($num); break; case 1: $num = rand(97, 122); // a-z 随机取一个 $rand = $rand.chr($num); break; case 2: $num = rand(65, 90); // A-Z 随机取一个 $rand = $rand.chr($num); break; } } $im=imagecreatetruecolor(100,40);//默认为黑色 $wordColor=imagecolorallocate($im,55,255,128); //线条 for($i = 0; $i < 3; $i++){ $stringColor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imageline($im,rand(0,100),0,rand(0,100),30,$stringColor); } //黑点 for($i = 0; $i < 200; $i++){ imagesetpixel($im, rand()%100, rand()%100, $stringColor); } imagestring($im, 6, rand(10,50), rand(5,15), $rand, $wordColor); header("Content-type:image/png"); imagepng($im); session_start(); $_SESSION["code"] = $rand; ?> ------------------------------------------- 05php_check_code.php <?php session_start(); $co = $_SESSION['code']; $qCo = $_POST['theCode']; $qCo = strtoupper($qCo); $co = strtoupper($co); if($co == $qCo){ echo "验证成功"; }else{ echo "验证失败"; } ?>
转载请注明原文地址: https://www.6miu.com/read-42447.html

最新回复(0)