Php user registration and set to logon status implementation method

Source: Internet
Author: User

The php tutorial describes how to register a user and set it to the logon status. The following example shows how to save the data submitted in the form to the mysql tutorial database without the function of automatically logging on after user registration, instance 2 implements this approach.

<Html>
<Body>
<? Php

$ Self = $ _ SERVER ['php _ SELF '];
$ Firstname = $ _ POST ['firstname'];
$ Lastname = $ _ POST ['lastname'];
$ Username = $ _ POST ['username'];
$ Password = $ _ POST ['Password'];

If ((! $ Firstname) or (! $ Lastname) or (! $ Username) or (! $ Password ))
{
$ Form = "Please enter all new user details ...";
$ Form. = "<form action =" $ self "";
$ Form. = "method =" post "> First Name :";
$ Form. = "<input type =" text "name =" firstname "";
$ Form. = "value =" $ firstname "> <br> Last Name :";
$ Form. = "<input type =" text "name =" lastname "";
$ Form. = "value =" $ lastname "> <br> User Name :";
$ Form. = "<input type =" text "name =" username "";
$ Form. = "value =" $ username "> <br> Password: & nbsp ;";
$ Form. = "<input type =" text "name =" password "";
$ Form. = "value =" $ password "> <br> ";
$ Form. = "<input type =" submit "value =" Submit "> ";
$ Form. = "</form> ";
Echo ($ form );
}
Else
{
$ Conn = @ mysql_connect ("localhost", "root", "") or die ("cocould not connect to MySQL ");
$ Db = @ mysql_select_db ("my_database", $ conn) or die ("cocould not select database ");
$ SQL = "insert into users (first_name, last_name, user_name, password) values (" $ firstname "," $ lastname "," $ username ", password ("$ password "))";
$ Result = @ mysql_query ($ SQL, $ conn) or die ("cocould not execute query ");
If ($ result ){
Echo ("New user $ username added ");
}
}
?>
</Body>


The following instance is more detailed. After the user registers and sets the user as the login status, this implementation uses setcookie to save the user login information

Create table user_info (
User_id char (18 ),
Fname char (15 ),
Email char (35 ));


// File: index. php

<?
$ Form ="
<Form action = "index. php" method = "post">
<Input type = "hidden" name = "seenform" value = "y">
Your first name? : <Br>
<Input type = "text" name = "fname" value = ""> <br>
Your email? : <Br>
<Input type = "text" name = "email" value = ""> <br>
<Input type = "submit" value = "Register! ">
</Form>
";
If ((! Isset ($ seenform ))&&(! Isset ($ userid ))):
Print $ form;
Elseif (isset ($ seenform )&&(! Isset ($ userid ))):
$ Uniq_id = uniqid (rand ());
@ Mysql_pconnect ("localhost", "root", "") or die ("cocould not connect to MySQL server! ");
@ Mysql_select_db ("user") or die ("cocould not select user database! ");
$ Query = "insert into user_info VALUES ('$ uniq_id', '$ fname',' $ email ')";
$ Result = mysql_query ($ query) or die ("cocould not insert user information! ");
Setcookie ("userid", $ uniq_id, time () + 2592000 );

Print "Congratulations $ fname! You are now registered !. ";
Elseif (isset ($ userid )):
@ Mysql_pconnect ("localhost", "root", "") or die ("cocould not connect to MySQL server! ");
@ Mysql_select_db ("user") or die ("cocould not select user database! ");
$ Query = "SELECT * FROM user_info WHERE user_id = '$ userid '";
$ Result = mysql_query ($ query) or die ("cocould not extract user information! ");

$ Row = mysql_fetch_array ($ result );
Print "Hi". $ row ["fname"]. ", <br> ";
Print "Your email address is". $ row ["email"];

Endif;

?>

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.