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: ";
$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
?>