本文目錄一覽:
怎樣用PHP和MYSQL製作一簡單的登錄界面網站
軟體:dreamwaver 記事本 也可以
mysql.php—-資料庫創建文件;
login.php—-前台用戶登陸文件;
check.php—-連接資料庫驗證文件;
mysql.php文件代碼:
程序代碼
?php
$db = mysql_connect(“localhost”,”root”,””) or die(“連接資料庫失敗!”);
if(!mysql_query(“create database if not exists `user`”))
{
echo “創建資料庫失敗br”;
}else
{
echo “創建資料庫成功!br”;
}
mysql_query(“use user;”);
$sql =”Create TABLE if not exists `user` (“
.” `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,”
.” `name` VARCHAR(10) NOT NULL,”
.” `password` VARCHAR(16) NOT NULL”
.” )”;
if(!mysql_query($sql))
{
echo “創建數據表失敗!br”;
}else
{
echo “創建數據表成功!br”;
}
$sql = “Insert INTO `user` ( `name`, `password`) VALUES ( ‘php-fish’, ‘123’);”;
if(mysql_query($sql))
echo “插入用戶成功!br”;
else
echo “插入失敗!br”;
mysql_close($db);
?
前台登陸文件login.php
程序代碼
form method=”post” action=”check.php”
table cellpadding=”0″ cellspacing=”0″ border=”0″ width=”25%”
tr
td用戶名:/td
tdinput type=”text” name=”name”/td
/tr
tr
td密碼:/td
tdinput type=”password” name=”password”/td
/tr
tr
tdinput type=”submit” value=”提交”/td
tdinput type=”reset” value=”清除”/td
/tr
/table
/form
用戶名:bphp-fish/b
br
密碼:b123br
驗證文件check.php
程序代碼
?php
if($_POST[‘name’] == “”)
{
echo “請填寫用戶名bra href=’login.php’返回/a”;
}elseif($_POST[‘password’] == “”)
{
echo “請填寫密碼bra href=’login.php’返回/a”;
}else
{
$conn = mysql_connect(“localhost”,”root”,””)
or die(“不能連接資料庫”.mysql_error());
mysql_select_db(“user”)
or die (“不能連接到user”.mysql_error());
$sql = “select * from user where id=3”;
$result = mysql_query($sql);
$query = mysql_fetch_array($result);
mysql_close($conn);
if(($query[‘name’] == $_POST[‘name’]) ($query[‘password’] == $_POST[‘password’]))
{
echo “驗證成功!br”;
}else
echo “密碼錯誤br”;
echo “a href=’login.php’返回/a”;
}
?
求一用php寫的註冊和登錄頁面代碼
reg.php文件
?php
header(“Content-type:text/html;charset=utf-8”);
if($_POST){
$dsn = ‘mysql:dbname=1104javab;host=127.0.0.1’;
$user = ‘root’;
$password = ”;
try{
$pdo = new pdo($dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND = ‘SET NAMES \’UTF8\”)
);
}catch(Exception $e){
echo ‘錯誤’.$e-getmessage();
}
$name = $_POST[‘name’];
$pwd = md5($_POST[‘pwd’]);
$sql = “insert into 表 (username,password) values (‘$name’,’$pwd’)”;
$exec = $pdo-query($sql);
if($exec){
echo “scriptalert(‘成功’);location.href=’reg.html’/script”;
}else{
echo “scriptalert(‘失敗’);location.href=’reg.html’/script”;
}
}
?
reg.html文件
form action=’reg.php’ method=’post’
用戶名:input type=’text’ name=’name’
密碼:input type=’password’ name=’pwd’
input type=’submit’ value=’submit’
/form
login.html文件
form action=’reg.php’ method=’post’
用戶名:input type=’text’ name=’name’
密碼:input type=’password’ name=’pwd’
input type=’submit’ value=’submit’
/form
login.php文件
header(“Content-type:text/html;charset=utf-8”);
if($_POST){
$dsn = ‘mysql:dbname=1104javab;host=127.0.0.1’;
$user = ‘root’;
$password = ”;
try{
$pdo = new pdo($dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND = ‘SET NAMES \’UTF8\”)
);
}catch(Exception $e){
echo ‘錯誤’.$e-getmessage();
}
$name = $_POST[‘name’];
$pwd = $_POST[‘pwd’];
$sql = “select user_id from 表名 where username=’$name’ and password=’$pwd'”;
$stmt = $pdo-query($sql);
$info = $stmt-fetch(PDO::FETCH_ASSOC);
if($info){
echo “登錄成功”;
}else{
echo “登錄失敗”;
}
}
大概這樣
求一個PHP登陸頁面代碼
改完了
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /
title這裡是網站的標題/title
link href=”css/index.css” rel=”stylesheet” type=”text/css”
style type=”text/css”
!–
body,td,th {
font-size: 14px;
}
a:link {
text-decoration: none;
color: #FFFFFF;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
.STYLE99 {
font-family: Georgia, “Times New Roman”, Times, serif;
color: #FF00FF;
}
—
/style/head
body
div id=”bigbox” class=”ceng”
div id=”banner” class=”ceng”img src=”images/banner.gif” width=”778″ height=”70″ //div
div id=”main” class=”ceng”
form id=”form1″ name=”form1″ method=”post” action=”post.php”
div id=”log”
div id=”logxx”span class=”dazi”這裡是網站的標題/span/div
div id=”yonghu”用戶名:
input name=”user” type=”text” id=”user” /
/div
div id=”mima”密 碼:
input name=”pass” type=”password” id=”pass” /
/div
div id=”yanzheng”驗證碼:
input name=”yz” type=”text” id=”yz” size=”8″ maxlength=”4″ /
?php
//$yzm = rand(1000, 9999 );
//echo $yzm;
$seedarray =microtime();
$seedstr =split(” “,$seedarray,5);
$seed =$seedstr[0]*10000;
//第二步:使用種子初始化隨機數發生器
srand($seed);
//第三步:生成指定範圍內的隨機數
$random =rand(1000,9999);
echo “span class=\”STYLE99\””.$random.”/span”;
?
input name=”hiddenField” type=”hidden” id=”hiddenField” value=”?php echo $random ?” /
/div
div id=”tijiao”
input type=”submit” name=”Submit” value=”提交” /
input type=”reset” name=”Submit2″ value=”重置” /
/div
/div
/form
/div
?php include(“buttom.php”);?
/div
/body
/html
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”
html xmlns=””
head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /
title這裡是網站的標題 /title
link href=”css/index.css” rel=”stylesheet” type=”text/css”
style type=”text/css”
!–
body,td,th {
font-size: 14px;
}
a:link {
text-decoration: none;
color: #FFFFFF;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
—
/style/head
body
div id=”bigbox” class=”ceng”
div id=”banner” class=”ceng”img src=”images/banner.gif” width=”778″ height=”70″ //div
div id=”main” class=”ceng”
div id=”log”
div id=”logxx”span class=”dazi”這裡是網站的標題/span/div
div id=”yonghu”
?php
function make_safe($variable) {
$variable = addslashes(trim($variable));
return $variable;
}
$user=make_safe($_REQUEST[“user”]);
$pass=make_safe($_REQUEST[“pass”]);
$yz=make_safe($_REQUEST[“yz”]);
$yzma=make_safe($_REQUEST[“hiddenField”]);
if ($yz == $yzma)
{
if ($user==”” or $pass==”” or $yz ==””)
{
echo”你輸入的信息有空,請a href=\”login.php\””.”返回”.”/a重新輸入”;
}
else
{
mysql_connect(“localhost”,”root”,”12345″) /*請修改用戶名和密碼*/
or die(“無法連接資料庫,請重來”);
mysql_select_db(“user”)
or die(“無法選擇資料庫,請重來”);
mysql_query(“SET NAMES ‘gbk'”);/*解決漢字*/
$row = mysql_fetch_assoc(mysql_query(” SELECT pass,xh FROM yh where name = ‘$user’ and password = ‘$pass'”));
$mima=$row[password];
if($pass == $mima)
{
session_start();
$_SESSION[‘yhm’]=$user;
echo “scriptalert(‘成功登陸’)/script”;
echo “scriptwindow.location.href=’a.php’;/script”;
}
else
{
echo”你的用戶名或者密碼輸入錯誤,請a href=\”login.php\””.”返回”.”/a”;
}
}
}
else
{
echo”您輸入的驗證碼不正確!請a href=\”login.php\””.”返回”.”/a”;
}
?
/div
div id=”mima”/div
div id=”yanzheng”/div
div id=”tijiao”/div
/div
/div
?php include(“buttom.php”);?
/div
/body
/html
求寫個比較簡單的php登陸頁面代碼
index.php 錄入頁代碼form id=”form1″ name=”form1″ method=”post” action=”login.php”
table width=”400″ border=”0″ align=”center” cellpadding=”1″ cellspacing=”1″ class=”tableborder”
tr
tddiv align=”right”用戶名:/div/td
td width=”244″
input type=”text” name=”User_name” id=”User_name” / /td
/tr
tr
tddiv align=”right”密 碼:/div/td
tdinput type=”Password” name=”User_Password” id=”User_Password” //td
/tr
tr
td
div align=”right”
input type=”submit” name=”button” id=”button” value=”提交” /
/div/td
tdinput type=”reset” name=”button2″ id=”button2″ value=”重置” //td
/tr
/table
/form login.php提交頁代碼?
error_reporting(0);
$mysql_servername = “127.0.0.1”;
$mysql_username = “root”;
$mysql_password =”123456″;
$mysql_database =”first”;
mysql_connect($mysql_servername , $mysql_username , $mysql_password);
mysql_select_db($mysql_database);
$U_name=$_POST[‘user_name’];
$U_passowrd=$_POST[‘user_password’];
if ($U_name $U_passowrd){
$sql = “SELECT * FROM admin WHERE U_name = ‘$U_name’ and U_password=’$U_passowrd'”;
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows){
header(“location=’c.php'”);
exit;
}
echo “script language=javascriptalert(‘用戶名密碼錯誤’);history.back();/script”;
}else {
echo “script language=javascriptalert(‘用戶名密碼不能為空’);history.back();/script”;
}
?
原創文章,作者:NUNS,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/146695.html