PHP user registration and set to login status implementation method

Source: Internet
Author: User
Tags mysql tutorial php tutorial setcookie

PHP Tutorial User Registration and set to the login status implementation method, the following example describes how to save the form submitted data to the MySQL Tutorial database tutorial, but not to implement the user registration automatically login function, and example II to achieve this practice.

<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 ("could not connect to MySQL");
$db = @mysql_select_db ("My_database", $conn) or die ("could 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 ("Could not execute query");
if ($result) {
Echo ("New user $username added");
}
}
?>
</body>


The following example is more detailed, after the user registers and sets the user's login status, this implementation utilizes the 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-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 ("could not connect to MySQL server!");
@mysql_select_db ("user") or die ("could not select User database!");
$query = "INSERT into User_info VALUES (' $uniq _id ', ' $fname ', ' $email ')";
$result = mysql_query ($query) or die ("could not insert user information!");
Setcookie ("userid", $uniq _id, Time () +2592000);

Print "Congratulations $fname! You are are now registered!. "
ElseIf (Isset ($userid)):
@mysql_pconnect ("localhost", "root", "") or Die ("could not connect to MySQL server!");
@mysql_select_db ("user") or die ("could not select User database!");
$query = "SELECT * from user_info WHERE user_id = ' $userid '";
$result = mysql_query ($query) or die ("Could 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.