Implementation method of php user registration and setting to logged-in status_PHP tutorial

WBOY
Release: 2016-07-13 17:05:50
Original
1154 people have browsed it

PHP tutorial user registration and set as logged in status implementation method, the following example describes how to save the data submitted in the form to the mysql tutorial database tutorial, but does not implement the function of automatic login after user registration, and the second example This approach was achieved.



$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.=" method="post">First Name: ";
$form.=" $form.=" value="$firstname">
Last Name: ";
$form.=" $form.=" value="$lastname">
User Name: ";
$form.=" $form.=" value="$username">
Password:   ";
$form.=" $form.=" value="$password">
";
$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");
}
}
?>


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

create table user_info (
user_id char(18),
fname char(15),
email char(35));


//File: index.php

$form = "



Your first name?:



Your email?:





";
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 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"].",
";
Print "Your email address is ".$row["email"];

endif;

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630755.htmlTechArticlephp tutorial user registration and setting as logged in status implementation method, the following example describes how to save the data submitted by the form Go to the mysql tutorial database tutorial, but after user registration is not implemented...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template