{"id":217,"date":"2009-12-22T16:45:30","date_gmt":"2009-12-22T16:45:30","guid":{"rendered":"http:\/\/dev001.dl\/yan\/wdp\/?p=217"},"modified":"2026-01-20T05:06:49","modified_gmt":"2026-01-20T05:06:49","slug":"php-session","status":"publish","type":"post","link":"http:\/\/www.lalife.net\/?p=217","title":{"rendered":"PHP session"},"content":{"rendered":"<p><strong>Use both Session and cookie to keep security for the website<\/strong><br \/>\n$_COOKIE[session_name()]<br \/>\nthe session name is stored as cookie, other variables are stored as session var<\/p>\n<p>auth.php<\/p>\n<p>&lt;?php<br \/>\nsession_set_cookie_params(7200,&#8217;\/&#8217;,&#8221;,true);<br \/>\nsession_start();<br \/>\nif($_SESSION[&#8216;auth&#8217;]!=&#8217;xxx&#8217;){<br \/>\nsession_destroy();<br \/>\nheader(&#8220;Location: .\/login.php\\n\\n&#8221;);<br \/>\nexit;<br \/>\n}else{<br \/>\n\/\/check fingerprint<br \/>\n$fp = $_SERVER[&#8220;HTTP_USER_AGENT&#8221;];<br \/>\n$fp .= $_SERVER[&#8220;REMOTE_ADDR&#8221;];<\/p>\n<p>$fp = md5($fp);<br \/>\nif($_SESSION[&#8216;par&#8217;] != $fp){<br \/>\nheader(&#8220;Location: .\/login.php\\n\\n&#8221;);<br \/>\nexit;<br \/>\n}<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>login.php<br \/>\ncheck if ID, pass are matched with those in db&#8230;.then<\/p>\n<p>session_start();<br \/>\n$_SESSION[&#8216;user_id&#8217;] = $uid;<br \/>\n$_SESSION[&#8216;grp_id&#8217;] = $gid;<br \/>\n$_SESSION[&#8216;auth&#8217;] = 1;<\/p>\n<p>$fp = $_SERVER[&#8220;HTTP_USER_AGENT&#8221;];<br \/>\n$fp .= $_SERVER[&#8220;REMOTE_ADDR&#8221;];<br \/>\n$_SESSION[&#8216;remote&#8217;] = md5($fp);<br \/>\nheader(&#8220;Location: .\/index.php&#8221;);<\/p>\n<p>logout.php<\/p>\n<p>session_set_cookie_params(7200,&#8217;\/&#8217;,&#8221;,true);<br \/>\nsession_start();<br \/>\n$_SESSION = array();<br \/>\nif(isset($_COOKIE[session_name()])){<br \/>\n$params = session_get_cookie_params();<br \/>\nsetcookie(session_name(), &#8221;, time()-3600,<br \/>\n$params[&#8220;path&#8221;], $params[&#8220;domain&#8221;],<br \/>\n$params[&#8220;secure&#8221;], $params[&#8220;httponly&#8221;]<br \/>\n);<br \/>\n}<br \/>\nsession_destroy();<br \/>\nheader(&#8220;Location: .\/login.php\\n\\n&#8221;);<br \/>\nexit;<\/p>\n<p>session_destroy() destroy the session, and cookie will be deleted as setting the cookie lifetime as a time past<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use both Session and cookie to keep security for the website $_COOKIE[session_name()] the session name is stored as cookie, other variables are stored as session var auth.php &lt;?php session_set_cookie_params(7200,&#8217;\/&#8217;,&#8221;,true); session_start(); if($_SESSION[&#8216;auth&#8217;]!=&#8217;xxx&#8217;){ session_destroy(); header(&#8220;Location: .\/login.php\\n\\n&#8221;); exit; }else{ \/\/check fingerprint $fp = &hellip; <a href=\"http:\/\/www.lalife.net\/?p=217\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[47],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-php","tag-php-session"],"_links":{"self":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=217"}],"version-history":[{"count":1,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":1953,"href":"http:\/\/www.lalife.net\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions\/1953"}],"wp:attachment":[{"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lalife.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}