簡單的php實現用戶的註冊和登入

環境php 5.2.13  mysql 5.5.3php

 

建立register.htmlhtml

 

<html>mysql

<body>sql

<form name="register" action="register.php" method="POST">ide

註冊用戶名<input type="text" name="name"/>測試

<p>密碼<input type="password" name="password"/> </p>ui

<input name="log" type="submit" value="註冊">.net

<input name="log" type="submit" value="登入">orm

</form>htm

</body>

</html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

建立register.php

 

<?php header("content-type:text/html; charset=utf-8"); ?>

<?php

$mysqli=mysqli_connect("localhost","root","******","test");

$name=$_POST["name"];

$password=$_POST["password"];

$hashpw=md5($password);

if ($name && $password){

$sql = "SELECT * FROM login WHERE user='$name'";

$res = mysqli_query($mysqli,$sql);

$rows=mysqli_num_rows($res);

if($rows){

echo "已有人註冊此名,請從新選擇名字!";

echo "<a href=register.html>返回</a>";

exit;

}

else{

$ins = "insert into login values('$name','$hashpw')";

$result = mysqli_query($mysqli,$ins);

if($result){

echo "祝賀你,註冊成功!";

echo "<a href=login.html>登入</a>";

exit;}

}

mysqli_free_result($res);

mysqli_close($mysqli);

}

?>

 

建立 login.html

 

<html>

<body>

<form name="login" action="login1.php" method="POST">

用戶名<input type="text" name="name"/>

<p>密碼<input type="password" name="password"/> </p>

<input name="log" type="submit" value="登陸">

</form>

</body>

</html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

建立login1.php

<?php

$mysqli=mysqli_connect("localhost","root","******","test");

$name=$_POST["name"];

$password=$_POST["password"];

$hashpw=md5($password);

if ($name && $password){

$sql = "SELECT * FROM login WHERE user='$name' and password='$hashpw'";

$res = mysqli_query($mysqli,$sql);

$rows=mysqli_num_rows($res);

if($rows){

header("location:index.php");

exit;

}

 else{

echo "登入失敗,請驗證!";

}

mysqli_free_result($res);

mysqli_close($mysqli);

}

?>

 

經過以上代碼能夠知道實現簡單的註冊和登入測試,能夠了解PHP和mysql簡單的調用和交互過程。

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/ydt619/archive/2010/09/16/5888856.aspx

相關文章
相關標籤/搜索