Original: PHP use session, to achieve user login and return to the clicked page (for example, TP for this article)

Source: Internet
Author: User

1, the following content is purely original, please choose carefully:

① Purpose: User Login timeout, session expires, click to jump to the login page, login success and then jump to the mouse click on the page.

② process: User Login---session expires---Click to jump to the login page---login successfully again---Jump back to the clicked page

③ idea: Using TP to bring the session by default, as long as our URL changes, so long will change the URL into the "same name" in the session variable;

User Login expires, session expires, click on any URL, the URL is re-stored in the session variable, login successfully, redirect to the URL can be

④ Code:

HTML page:

<script src= "{$TMPL}/js/ajax_user_login.js" ></script><form id= "User_login_form" Name= "User_login_ Form "action= ' {URL r=" user#do_login "} ' ><span class=" lh40 w100 mr20 "><i class=" color-red ">* </i> Account </span> <input name= "email" id= "email"  placeholder= "phone number/member name/email" type= "text"   class= "textbox"/ ><span class= "lh40 w100  mr20" ><i class= "color-red" >* </i> password </span><input  Name= "User_pwd" id= "user_pwd" placeholder= "Please enter password" type= "password" class= "textbox"/><input type= "button" value= "Login" name= "Submit_form" id= "Btn_do_login"/><input type= "hidden" value= "1" name= "Ajax"/></form>

  

Js:

$ (document). Ready (function () {Do_login_user ();}); function Do_login_user () {var Ajaxurl = $ ("form[name= ' User_login_form ')"). attr ("action"); var query = $ ("form[name=" User_login_form '] "). Serialize (); $.ajax ({url:ajaxurl,datatype:" JSON ", Data:query,type:" POST ", Success:function ( Ajaxobj) {if (ajaxobj.status==1) {location.href = Ajaxobj.jump;   The URL of the session address that the PHP passed over});

PHP code (TP framework):

The core BaseModule.class.php:

Class Basemodule{public function __construct () {/       * system Determines whether session-xzz0419 is turned on by default */    if (!isset ($_session)) {    Session_Start ();    }        Set_gopreview ();     The current page URL is stored in session ();        /* Other code */}    

PHP's Common method:

function Get_current_url () {$url = $_server[' Request_uri '). ( Strpos ($_server[' Request_uri '), '? ')? ': '? '); $parse = Parse_url ($url); if (Isset ($parse [' query '])) { parse_str ($parse [' query '], $params); $url = $parse [' Path ']. Http_build_query ($params); } return $url;}

function Set_gopreview ()
{
$url = Get_current_url ();
Session ("Gopreview", $url);
}

function Get_gopreview ()
{
$gopreview =session ("Gopreview");

if (!isset ($gopreview) | | $gopreview = = "")
{
$gopreview = Session (' Before_login ') session (' Before_login '): URL ("index");
}
Session ("Before_login", null);
Session ("Gopreview", null);
return $gopreview;
}

PHP Processing Login Function code:

Public Function Do_login () {if (!$_post) {app_redirect (app_root. ") /");} foreach ($_post as $k = = $v) {$_post[$k] = Strim ($v);} $ajax = intval ($_request[' Ajax ')), $jump _url = Get_gopreview ();//Gets the clicked URL, has been stored in the session if ($ajax ==1) {$return [' status '] = 1; $return [' info '] = "Login Successful", $return [' data '] = $result [' msg ']; $return [' jump '] = $jump _url;ajax_return ($return);         }}

  

Original: PHP use session, to achieve user login and return to the clicked page (for example, TP for this article)

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.