代码
dbtest.sql
1 CREATE DATABASE `dbtest` ; 2 CREATE TABLE `dbtest`.`users` ( 3 `user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , 4 `username` VARCHAR( 25 ) NOT NULL , 5 `email` VARCHAR( 35 ) NOT NULL , 6 `password` VARCHAR( 50 ) NOT NULL , 7 UNIQUE (`email`) 8 ) ENGINE = MYISAM ;
dbconnect.php
1 <?php 2 if(!mysql_connect("localhost","root","root")) 3 { 4 die(‘oops connection problem ! --> ‘.mysql_error()); 5 } 6 if(!mysql_select_db("dbtest")) 7 { 8 die(‘oops database selection problem ! --> ‘.mysql_error()); 9 } 10 ?>
index.php
1 <?php 2 session_start(); 3 include_once ‘dbconnect.php‘; 4 5 if(isset($_SESSION[‘user‘])!="") 6 { 7 header("Location: home.php"); 8 } 9 if(isset($_POST[‘btn-login‘])) 10 { 11 $email = mysql_real_escape_string($_POST[‘email‘]); 12 $upass = mysql_real_escape_string($_POST[‘pass‘]); 13 $res=mysql_query("SELECT * FROM users WHERE email=‘$email‘"); 14 $row=mysql_fetch_array($res); 15 if($row[‘password‘]==md5($upass)) 16 { 17 $_SESSION[‘user‘] = $row[‘user_id‘]; 18 header("Location: home.php"); 19 } 20 else 21 { 22 ?> 23 <script>alert(‘wrong details‘);</script> 24 <?php 25 } 26 27 } 28 ?> 29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 30 <html xmlns="http://www.w3.org/1999/xhtml"> 31 <head> 32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 33 <title>cleartuts - Login & Registration System</title> 34 <link rel="stylesheet" href="style.css" type="text/css" /> 35 </head> 36 <body> 37 <center> 38 <div id="login-form"> 39 <form method="post"> 40 <table align="center" width="30%" border="0"> 41 <tr> 42 <td><input type="text" name="email" placeholder="Your Email" required /></td> 43 </tr> 44 <tr> 45 <td><input type="password" name="pass" placeholder="Your Password" required /></td> 46 </tr> 47 <tr> 48 <td><button type="submit" name="btn-login">Sign In</button></td> 49 </tr> 50 <tr> 51 <td><a href="register.php">Sign Up Here</a></td> 52 </tr> 53 </table> 54 </form> 55 </div> 56 </center> 57 </body> 58 </html>
home.php
1 <?php 2 session_start(); 3 include_once ‘dbconnect.php‘; 4 5 if(!isset($_SESSION[‘user‘])) 6 { 7 header("Location: index.php"); 8 } 9 $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION[‘user‘]); 10 $userRow=mysql_fetch_array($res); 11 ?> 12 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 13 <html xmlns="http://www.w3.org/1999/xhtml"> 14 <head> 15 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 16 <title>Welcome - <?php echo $userRow[‘email‘]; ?></title> 17 <link rel="stylesheet" href="style.css" type="text/css" /> 18 </head> 19 <body> 20 <div id="header"> 21 <div id="left"> 22 <label>cleartuts</label> 23 </div> 24 <div id="right"> 25 <div id="content"> 26 hi‘ <?php echo $userRow[‘username‘]; ?> <a href="logout.php?logout">Sign Out</a> 27 </div> 28 </div> 29 </div> 30 </body> 31 </html>
register.php
1 <?php 2 session_start(); 3 if(isset($_SESSION[‘user‘])!="") 4 { 5 header("Location: home.php"); 6 } 7 include_once ‘dbconnect.php‘; 8 9 if(isset($_POST[‘btn-signup‘])) 10 { 11 $uname = mysql_real_escape_string($_POST[‘uname‘]); 12 $email = mysql_real_escape_string($_POST[‘email‘]); 13 $upass = md5(mysql_real_escape_string($_POST[‘pass‘])); 14 15 if(mysql_query("INSERT INTO users(username,email,password) VALUES(‘$uname‘,‘$email‘,‘$upass‘)")) 16 { 17 ?> 18 <script>alert(‘successfully registered ‘);</script> 19 <?php 20 } 21 else 22 { 23 ?> 24 <script>alert(‘error while registering you...‘);</script> 25 <?php 26 } 27 } 28 ?> 29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 30 <html xmlns="http://www.w3.org/1999/xhtml"> 31 <head> 32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 33 <title>Login & Registration System</title> 34 <link rel="stylesheet" href="style.css" type="text/css" /> 35 36 </head> 37 <body> 38 <center> 39 <div id="login-form"> 40 <form method="post"> 41 <table align="center" width="30%" border="0"> 42 <tr> 43 <td><input type="text" name="uname" placeholder="User Name" required /></td> 44 </tr> 45 <tr> 46 <td><input type="email" name="email" placeholder="Your Email" required /></td> 47 </tr> 48 <tr> 49 <td><input type="password" name="pass" placeholder="Your Password" required /></td> 50 </tr> 51 <tr> 52 <td><button type="submit" name="btn-signup">Sign Me Up</button></td> 53 </tr> 54 <tr> 55 <td><a href="index.php">Sign In Here</a></td> 56 </tr> 57 </table> 58 </form> 59 </div> 60 </center> 61 </body> 62 </html>
logout.php
1 <?php 2 session_start(); 3 4 if(!isset($_SESSION[‘user‘])) 5 { 6 header("Location: index.php"); 7 } 8 else if(isset($_SESSION[‘user‘])!="") 9 { 10 header("Location: home.php"); 11 } 12 13 if(isset($_GET[‘logout‘])) 14 { 15 session_destroy(); 16 unset($_SESSION[‘user‘]); 17 header("Location: index.php"); 18 } 19 ?>
时间: 2024-10-09 21:18:31