Access to the PHP login session

Source: Internet
Author: User
This article mainly introduces the PHP login session to obtain, has a certain reference value, now share to everyone, the need for friends can refer to

Log session when logging in

/** * Execute login */public function Dologin () {$condition [' name '] = $username = Trim ($_post[' username ']); $condition [' Password '] = $password = MD5 (Trim ($_post[' password '));//$imgCode = $_post[' Imgcode '];if (Empty ($username) | | empty ( $password) {$this->ajaxreturn (null,c ("err_msg_70"), "Success:false");} $user = D ("user")->relation (True)->where ($condition)->find (), if (Empty ($user)) {$this->ajaxreturn (null , "User name or password error", "Success:false");} if (Empty ($user [' apps ') && $user [' role ']! = usermodel::admin) {$this->ajaxreturn (null, ' The user does not belong to any product line, Do not allow login, please contact the administrator! ', ' Success:false ');} if (empty ($user)) {$this->ajaxreturn (null,c ("err_msg_70"), "Success:false");} $defaultAppId = $user [' defaultapp '] >= 0? $user [' Defaultapp ']: $user [' Apps '][0][' id '];foreach ($user [' apps '] as $app) {if ($app [' id '] = = $defaultAppId) {$appName = $app [' AppName '];break;}} $session = Array (' uid ' = = $user [' id '], ' username ' + $username, ' role ' = ' $user ' [' Role '], ' appId ' = ' $ ' Defaultappid, ' appName ' =$appName); setsession ($session); $this->ajaxreturn ($data, "Congratulations, login successful! "," Success:true ");}

When a specific page is used, gets the session in the action

 Public function Deploy () {$username = Session (' username ');        $conditions = Explode (",", $_post[' environment ');        $envarr = Array (); foreach ($conditions as $condition) {$envIds = D (' Env ')->field (' Name,ip ')->where ("Name L Ike ' ". $condition.                        "% '")->select ();            foreach ($envIds as $key = + $envId) {$envIds [$key] [' mem '] = $this->getcpumem ($envId [' IP ']);            } $envIdsSort = $this->my_sort ($envIds, ' mem ', SORT_ASC, sort_string);            $envIds =array_remove ($envIds, ' mem ');            $envarray [' env '] = $condition;            $envarray [' smallip '] = $envIdsSort [0][' IP '];        $envarr [] = $envarray;        } $_post[' Environment ' = Json_encode ($envarr);                $_post[' username ']= $username;        $resultnew = $this->request ("localhost:8080/execute/isbuildingnew.html", $_post, "POST"); Var_dump ($resuLenew); if ($resultnew = = "Building:true") {$this->ajaxreturn (1, ' previous build is in progress, please try again later!)        ', ' success:false ');            } else {$result = $this->request ("localhost:8080/execute/onlinedeploynew.html", $_post, "POST");            if ($result) {$this->ajaxreturn (1, $result, ' success:true ');            } else {$this->ajaxreturn (0, "Commit build Failed", ' success:false '); }        }    }

Then the backend will be able to get to this username, soeasy!

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.