Build a mobile User login mechanism that does not depend on cookies

Source: Internet
Author: User

The problem with the mobile login site is a problem that has plagued me for a long time. There will always be some users telling me that his phone can't log on to the website. In order to effectively solve the problem of mobile phone login, one solution is to simulate the user's mobile environment as much as possible, and to test and adjust the code for these specific environments. Another option is to make the site as free as possible from relying on the client cookie environment to create more robust code.

With the development of information technology, the mobile phone itself hardware equipment more and more advanced, the function is more and more comprehensive, the support for the cookie is obviously not a problem. However, there is always a part of the population in the use of relatively low-end mobile phones. As a developer, to the maximum extent possible to meet the needs of different mobile phone users, you have to consider the support of mobile phones that do not support cookies. Otherwise, you may have to tell users over and over that our website supports a mobile phone with a cookie feature and even has to explain to users what a cookie is for a half day.

So, let's look at what we need to do to create a user login mechanism that doesn't depend on cookies.

First, understand the support of PHP for the session mechanism

Several useful fields in php.ini about the session:

Session.use_trans_sid enables support for passing SessionID via URL parameters by turning on this parameter. The default value is 0 and does not turn on.

Url_rewriter.tags which places need to be used to pass SessionID. The default value is "A=href,area=href,frame=src,input=src,form=fakeentry", which means that in the label a=href the href attribute for label A is appended to the SessionID.

The default value for session.use_cookies is "1", which means that the SessionID uses a cookie to pass it, and the other is to use the URL parameter. If session.use_trans_sid=1, then enable this option, while the client supports cookies, still uses the cookie to pass the SessionID, in the case that the client does not support cookies, The SessionID is passed using the URL parameter.

Session.name This is sessionid stored variable name, default is PHPSESSID

Session.cookie_lifetime This represents SessionID in the client cookie storage time, the default is 0, on behalf of the browser a close SessionID is void.

Session.gc_maxlifetime This is the time the session data is stored on the server side, and if this time is exceeded, the session data is automatically deleted.

The save path of the Session.save_path session. The default is/tmp. If you add 2 before the path, the cookie has a storage depth of 2. For example, if session.sava_path=2; /tmp/session, then sess_005c685e3ec4e0cf6b667245225299c8 this session will be saved in/tmp/session/0/0/this directory.

Ii. Basic methods of use

Create a new session.php file with the following code:

<a href= "hello.php" >hello</a>

At the end of the output, the link address is definitely hello.php, so let's add a few lines of code to enable it to support passing SessionID.

<?phpini_set (' Session.use_trans_sid ', 1);? ><a href= "hello.php" >hello</a>

The link still doesn't change when you see it in the browser. What's going on here? The original server side in the detection of browser-side support cookie is not turned on the URL to pass SessionID.

Try disabling cookies for your browser. Will find the effect is still. What's going on here?

The fact is, to let the server support the replacement of the link address, first of all to let the server get to SessionID. Then we're going to open the session. The Session_Start function will be used at this time. Then modify the code as follows:

<?phpini_set (' Session.use_trans_sid ', 1); Session_Start ();? ><a href= "/hello.php" >hello</a> Refresh the interface, and find that the generated source code has become the following.
<a href= "/hello.php? Phpsessid=f10962d83fdfb27b501b53e099916a90 ">hello</a>

So congratulations, our goal has basically been completed.

In the previous code, we used the Ini_set function directly in the code to set the PHP.ini field, what good would that do? Because in a real-world server environment, multiple Web sites may run, modifying the configuration in php.ini directly affects the operation of other sites, but setting them up in code can weaken the effect to the fullest extent.

Third, how to ensure the various label transfer SessionID

In the previous example, we have been able to properly append the href attribute of the A tag to the SessionID. However, in the actual development, there may be more complex situations. For example, you might use a form, or you might use an IFRAME, even if you want to pass a SessionID even when using a picture. So take a look at the following code:

<?php    ini_set (' Session.use_trans_sid ', 1);    Session_Start ();? ><a href= "/hello.php" >hello</a><form><input value= "Submit"/></form><iframe Src= "/hello.php" ></iframe>

After the interface is refreshed, the output code is:

<a href= "/hello.php? phpsessid=a1ff38e07c28474b80766ef96ad88013 ">hello</a>    <form><input type=" hidden "name=" Phpsessid "value=" a1ff38e07c28474b80766ef96ad88013 "/>    <input type=" Submit "value=" Submit "/>    < iframe src= "/hello.php" ></iframe>    

In other words, the form, has been able to pass the SessionID well, but for the IFRAME and IMG, but still can not pass SessionID. What do we do? Do we need to assign values manually?

Actually, in the first part, we've mentioned a useful field for--url_rewriter.tags. We are trying to increase its support for IFRAME and IMG. The code is as follows:

<?php    ini_set (' Session.use_trans_sid ', 1);    $tags = Ini_get (' url_rewriter.tags ');    Ini_set (' Url_rewriter.tags ', $tags. ', Iframe=src,img=src ');    Session_Start ();? ><a href= "/hello.php" >hello</a><form><input value= "Submit"/></form><iframe Src= "/hello.php" ></iframe>

The output results are as follows:

<a href= "/hello.php? phpsessid=a1ff38e07c28474b80766ef96ad88013 ">hello</a><form><input type=" hidden "name=" Phpsessid "value=" a1ff38e07c28474b80766ef96ad88013 "/>  <input type=" Submit "value=" Submit "/></form ><iframe src= "/hello.php? phpsessid=a1ff38e07c28474b80766ef96ad88013 "></iframe>

It is clear that the IFRAME and IMG are also able to pass the SessionID smoothly.

So far, we've been able to solve most of the problems.

Iv. transmission in the case of redirection

In the second part, we've been able to get rid of the SessionID of the page output, but that's not all. For example, we often redirect pages by setting the header.

Header (' Location:/hello.php ');

In this case, it is clear that PHP is not going to handle it. Well, we still need to make the adjustments ourselves manually. The code is as follows:

<?php    ini_set (' Session.use_trans_sid ', 1);    $tags = Ini_get (' url_rewriter.tags ');    Ini_set (' Url_rewriter.tags ', $tags. ', Iframe=src,img=src ');    Session_Start ();    function Get_redirect_url ($url) {        $sessionkey = ini_get (' session.name ');        $sessionId = session_id ();        if (Strpos ($url, '? ')!== false) {            $tag = ' & ';        } else {            $tag = '? ';        }        $url. = $tag. $sessionKey. ' = '. $sessionId;        return $url;    }    $rurl = Get_redirect_url ('/hello.php ');    Header (' Location: '. $rurl);? >

With the Get_redirect_url function, we can also support the delivery of the redirected URL SessionID.

Too good to solve a situation.

V. Special circumstances of the wap1.x

However, there are some special cases when using WML development under wap1.x. For example, the URL of the & needs to be converted to &amp, otherwise the browser will explain the error. Therefore, for page labels and redirects, we need to do special processing for wap1.x pages.

The redirection situation is well handled, and the fourth part of the Get_redirect_url function in $tag= ' & ' is replaced with $tag= ' &amp; ' in the wap1.x environment Can. However, for the interface label processing, we need to introduce a new configuration item--arg_separator.output. This configuration item is used to specify what hyphen to use when adding the SessionID parameter to the URL, and the default value is &. Then, when the wap1.x environment is known, add the following statement to squeaked:

Ini_set (' arg_separator.output ', ' &amp; ');

Well, so far, we've basically solved most of the use of URL-passing SessionID in WAP development. Seemingly more content, in fact, the HTML tag is introduced in how to pass SessionID and how to pass the redirect when the SessionID two cases, and the situation in the wap1.x environment to do some special treatment, and ultimately ensure that the The process by which a cookie-dependent user login mechanism can be implemented. I hope this summary of the content for the WAP developers can bring some help.

Build a mobile User login mechanism that does not depend on cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.