PHP method using an infinite life period session

zhaozj2021-02-16  98

In PHP4.0, the support for Session is added to facilitate our many programs, such as shopping carts, and more.

In many forums, session is also used to process users' logins, record username and password, so that users don't have to enter their username and password every time! However, the general session life is limited. If the user closes the browser, it cannot save the SESSION variable! So how can I achieve the permanent life of the session?

Everyone knows that session is stored in the server side, obtain this user's file according to the sessionId provided by the client, and then reads the file, obtains the value of the variable, the sessionID can use the client's cookie or HTTP1.1 protocol Query_String (That is the URL of access) "?" The following part) to transfer to the server, then the server reads the session directory ...

To implement the permanent life of the session, you first need to understand the PHP.INI's related settings for the session (open the php.ini file, "[session]" section):

1, session.use_cookies: The default value is "1", representing the sessionID to pass using cookies, and then use query_string to pass;

2, session.name: This is the variable name stored by sessionID, which may be cookie, or query_string to pass, the default value is "phpsessid";

3, session.cookie_lifetime: This represents SessionID's time in the client cookie, the default is 0, representing the browser one closed sessionID is a waste ... is because this SESSION cannot be used forever!

4, session.gc_maxlifetime: This is the time of session data in server-side, if it exceeds this time, then session data is automatically deleted!

There are still many settings, but it is related to this article, and the principles and steps of permanent session began below.

As mentioned earlier, the server reads the session data by sessionid, but the sessionID transmitted by the general browser is not there after the browser is turned off, then we only need to manually set the sessionID and save it, no. ...

If you have a server's Cao as authority, then set this very simple, just need to perform the following steps:

1. Set "session.use_cookies" to 1, open the cookie to save the sessionID, but the default is 1, generally no modification;

2, change "session.cookie_lifetime" to be infinite (of course, there is no infinite parameter, but 999999999 and is endless);

3. Set the "session.gc_maxlifetime" set to "session.cookie_lifetime";

After the setting is completed, open the editor and enter the following code:

------------------------------------------

session_start ();

Session_register ('count');

$ count ;

Echo $ count;

?>

------------------------------------------ then saved as "session_check.php" Use the browser to open "session_check.php", see if it is "1", then close the browser, then open the browser to access "session_check.php", if "2" is displayed, then congratulations, you already Success; if you fail, check your front settings.

But if you don't have a server's Cao as authority, then it is more troublesome, you need to rewrite the sessionID through the PHP program to achieve permanent session data saving. Check the function manual of php.net, you can see "SESSION_ID" function: If no parameters are set, then the current sessionid, if the parameters are set, the current sessionID is set to the value given ...

Just use the permanent cookie plus the "session_id" function, you can save your permanent session data!

But for convenience, we need to know the "session.name" of the server settings, but the general user has no permission to view the server's php.ini settings, but PHP provides a very good function "PHPINFO", using this can see almost all PHP information!

----------------------------------

PHP Related Information Display </ Title></p> <p><? phpinfo ()?></p> <p>------------------------------------</p> <p>Open the editor, enter the above code, and then run this program in the browser, see information about PHP. One of these "session.name" parameters, this is the server "session.name" we need, generally "phpsessid".</p> <p>After you write down the name of the sessionID, we can achieve a permanent session data store!</p> <p>Open the editor and enter the following code:</p> <p>-------------------------------------------------- ---------------------------</p> <p><?</p> <p>session_start (); // Start session</p> <p>Session_register ('count'); // Register Session Variable COUNT</p> <p>IF (Isset ($ PHPSESSID)) {</p> <p>Session_id ($ PHPSESSID);</p> <p>} // If you set a $ pHPSESSID, you will assign the sessionID to $ PHPSESSID, otherwise generates sessionid</p> <p>$ PHPSESSID = session_id (); // Get the current sessionID</p> <p>$ count ; // Variable COUNT plus 1</p> <p>SetCookie ('phpsessid', $ PHPSESSID, TIME () 3156000); // Save SessionID to Cookie</p> <p>Echo $ count; // Display the value of session variable count</p> <p>?></p> <p>-------------------------------------------------- ------------------------------</p> <p>After saving, using the same method as the detection of server rights, detects whether the sessionID is successfully saved.</p> <p>postscript:</p> <p>In fact, the true permanent storage is impossible, because the Cookie has limited storage time, and the server's space is limited ... but for some sites that need to save time, the above method is already enough! Finally, the author's debugging environment: Windows98Digext (SE) Apache PHP 4.04.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-11800.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="11800" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.053</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '91sfuYgGUvnHpDU1IzfhHdraSx5AjuYwcrS1UmfUHWka9VDS_2FBvjaxhkC8JoV2hX7nbaatVlC73AULqm'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>