Full registration of PHP source code, with the session verification.

Source: Internet
Author: User
Tags php source code rtrim

1, first write the form page login.html.
<! DOCTYPE html>    <title> User Login </title></body>2, then write the login source code.
<?php session_start ();
Log Off Login function logout () {if ($_get[' action ']= ' logout ') {unset ($_session[' user_id ']);            unset ($_session[' username '); Echo ' Logout login successful! Click here <a href= "Javascript:history.back (-1);"            > Login </a> ';        Exit }    }
Login if (!isset ($_post[' submit ')) {exit (' illegal access!    ‘); }
$username = Htmlspecialchars ($_post[' username '); $password = MD5 ($_post[' password ');
Introduction of database file include (' mysql.php ');    Detects if the user name password is correct $where = ' username= '. $username. ' ";            if ($rs =mysql_fetch_assoc (' user ', $where)) {//Login succeeded $_session[' name '] = $username;            $_session[' userid '] = $rs [' user_id ']; echo $username, ' Welcome!            Enter <a href= "my.php" > User Center </a><br/> '; Echo ' Click here <a href= "Login.php?action=logout" > Logout </a> Login!            <br/> ';            Exit }else {exit ' Login failed! Click here <a href= "Javascript:history.back (-1);"        > Return </a> retry '); }
3. Registration page Form
<! DOCTYPE html></body>
4, receive the registration form.

<?php
if (!isset ($_post[' submit ')) {exit (' illegal operation!    ‘); }
$username = Strtolower ($_post[' username ');    $password = strtolower ($_post[' password ');    $email = strtolower ($_post[' email '); Registration information judgment//if (!preg_match ('/^[\w\x80-\xff]{3-15}$/', $username)) {//exit (' ERROR: User name does not meet the requirements. <a href= "Javascript:history.back (-1);"    > Return </a> '); } if (strlen ($password) <6) {exit (' ERROR: Password length is too short. <a href= "Javascript:history.back (-1);"    > Return </a> '); }//if (!preg_match ('/^w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$/', $email)} {//exit (' ERROR: E-mail format is incorrect. <a href= "Javascript:history.back (-1);"    > Return </a> '); // }
Introduce the database operation file include (' mysql.php ');//detect if the user name exists $where = ' username= '. $username. ' "; if (MYSQL_FETCH_ASSOC (' user ', $where)) {echo ' ERROR: Username ', $usename, ' already exists. <a href= "Javascript:history.back (-1) ;"        > Return </a> ';    Exit (); }
Insert Data $password = MD5 ($password);    $data [' username '] = $username;    $data [' password '] = $password;   $data [' email '] = $_post[' email '];   $re = insert (' user ', $data); if ($re) {exit (' User registered successfully!   Click here to login <a href= "login.html" > Login </a> ');        }else{echo ' Add data failed: ', mysql_error (), ' <br> '; Echo ' Click here <a href= "Javascript:history.back (-1);"   > Return </a> retry '; }
5, verify whether the page has been logged in <?phpsession_start ();
Check whether to log in, if not login to the login interface if (!isset ($_session[' userid ')) {header ("Location:login.html"); exit ();}
Include database connection file include (' conn.php '); $userid = $_session[' userid '); $username = $_session[' username ']; $user _query = Mysql_ Query ("SELECT * from user where uid= $userid limit 1"); $row = mysql_fetch_array ($user _query); Echo ' User information: <br/> '; echo ' User id: ', $userid, ' <br/> '; Echo ' username: ', $username, ' <br/> '; Echo ' email: ', $row [' email '], ' <br/> '; Echo ' Registration date: ', date (' y-m-d ', $row [' regdate ']), ' <br/> '; Echo ' <a href= ' login.php?action=logout ' > logout </a> Login <br/> ';
6, Operation database function. <?php
$db _host = "localhost:3306";//Database Name$db_name = "TEST";//Database Username$db_user = "root";//Database Pass Word$db_pass = "";
$con = mysql_connect ($db _host, $db _user, $db _pass), if (! $con) {Die (' Could not connect: '. Mysql_error ());} mysql_select_db ($db _name, $con);//Some code#mysql_close ($con), function Select ($table, $where = ", $order =", $limits = ", $field =") {
$field = ($field ==null)? ' * ': $field;$sele. = "from". $table;$where = ($where ==null)? ': ' Where '. $where;$order = ($order ==null)? ': ' Order by '. $order;$limits = ($limits ==null)? ': ' Limit '. $limits;$sele = "Select". $field. "from". $table. $where. $order. $limits;Var_dump ($sele);return mysql_query ($sele);Mysql_close ($con);} Insert, two parameters are required, after an array function insert ($table, $columns) {foreach ($columns as $key = $values){$addkey. = '. $key. ' ', ';if (Is_numeric ($values)){$addvalues. = $values. ', ';}Else{$values =mysql_real_escape_string ($values);$addvalues. = ' \ '. $values. ' \‘,‘;}}$addkey =rtrim ($addkey, ', ');$addvalues =rtrim ($addvalues, ', '); $inse. = ' INSERT into ' $table. ' ('. $addkey. ') VALUES ('. $addvalues. '); ';return mysql_query ($inse);} Update, three parameters are required function update ($table, $data, $where) {foreach ($data as $k = $v){if (Is_numeric ($v)){$edit _data.= '. $k. '. ' = '. $v. ', ';}Else{$edit _data.= '. $k. '. ' = '. ' ". Mysql_real_escape_string ($v). "',";}}$edit _data=rtrim ($edit _data, ', ');$UPDA = "UPDATE". $table. " SET ". $edit _data." WHERE ". $where;return mysql_query ($UPDA);} Delete function Delete ($table, $where) {$dele = "delete from". $table. " WHERE ". $where;return mysql_query ($dele);}
?>


From for notes (Wiz)

Full registration of PHP source code, with the session verification.

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.