導航:首頁 > 編程語言 > php登陸注冊教程

php登陸注冊教程

發布時間:2023-01-05 10:25:55

⑴ 用php做登陸注冊頁面

登錄頁:login.php
<?php
include("conn.php");
$username=$_POST['name'];
$password=$_POST['password'];
$yanzheng=$_POST['yanzheng'];

if(isset($_POST['submit']))
{
$sql=("select username,password from member where username='$username' and password='$password'") or die("sql語句執行失敗");
//print_r($sql);
$ar=mysql_query($sql);
if($ar)
{
if($row=mysql_fetch_array($ar))
{
session_start();
if($_POST["yanzheng"])
{
if($yanzheng!=$_session[pic]||$yanzheng=="")
{
echo "驗證碼輸入有誤";
exit;
}
if($yanzheng==$_session[pic])
{
header("location:index.php");
}
}
}
else
{
echo "用戶名或密碼錯誤";
}
}
}

?>

<form action="login.php" method="post">
<table border=1 align=center width=500 height=300 bgColor=#DFFFDF bordercolor=#fffbec>
<tr>
<td colspan=2 align=center>用戶登錄</td>
</tr>
<tr>
<td>用戶姓名:</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>用戶密碼:</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td>驗證碼:</td>
<td><input type="text" name="yanzheng" id="yanzheng"/>
<img src="yanzheng1.php" width="50" height="30"></img>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<input type="submit" name="submit" value="登錄"/>
<input type="reset" name="reset" value="重置"/>
<a href="register.php">注冊</a>
</td>
</tr>
</table>
</form>

注冊頁:register.php

<?php
include("conn.php");
if(isset($_POST['submit'])&&$_POST['submit']) {
if($_POST['username']=='')
{
echo "用戶名不能為空";
exit();
}
if($_POST['password']=='')
{
echo "密碼不能為空";
exit();
}
if($_POST['realpass']!=$_POST['password'])
{
echo "兩次密碼輸入不一致";
exit();
}

$sql="insert into member(username,real_name,password,email,headimg) values('$_POST[username]','$_POST[username]','$_POST[password]','$_POST[email]','')";
$ar=mysql_query($sql);
if($ar)
{
header("location:index.php");
}
else
{
echo mysql_error();
}
}
?>
<body>
<form action="register.php" method="post">
<table border=1 align=center width=500>
<tr>
<td height=40 bgColor=#DFFFDF colspan=2>會員注冊 [<a href="login.php">返回登錄頁</a>]</td>
</tr>
<tr>
<td height=40 bgColor=#fffbec >會員ID</td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>密碼</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>確認密碼</td>
<td>
<input type="password" name="realpass" id="realpass"/>
</td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>EMAIL</td>
<td><input type="text" name="email" id="email"/>

</tr>
<tr>
<td height=40 bgColor=#fffbec></td>
<td><input type="submit" name="submit" value="注冊"/><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>

主頁顯示:index.php
<?php
include("conn.php");
function cutstr($str,$cutleng)
{
$str = $str; //要截取的字元串
$cutleng = $cutleng; //要截取的長度
$strleng = strlen($str); //字元串長度
if($cutleng>$strleng)return $str;//字元串長度小於規定字數時,返回字元串本身
$notchinanum = 0; //初始不是漢字的字元數
for($i=0;$i<$cutleng;$i++)
{
if(ord(substr($str,$i,1))<=128)
{
$notchinanum++;
}
}
if(($cutleng%2==1)&&($notchinanum%2==0)) //如果要截取奇數個字元,所要截取長度范圍內的字元必須含奇數個非漢字,否則截取的長度加一
{
$cutleng++;
}
if(($cutleng%2==0)&&($notchinanum%2==1)) //如果要截取偶數個字元,所要截取長度范圍內的字元必須含偶數個非漢字,否則截取的長度加一
{
$cutleng++;
}
return substr($str,0,$cutleng);
}
?>
<html>
<head>
<script type="text/javascript">
function All(e, itemName)
{
var aa = document.getElementsByName(itemName);
for (var i=0; i<aa.length; i++)
aa[i].checked = e.checked; //得到那個總控的復選框的選中狀態
}
function Item(e, allName)
{
var all = document.getElementsByName(allName)[0];
if(!e.checked) all.checked = false;
else
{
var aa = document.getElementsByName(e.name);
for (var i=0; i<aa.length; i++)
if(!aa[i].checked) return;
all.checked = true;
}
}
</script>
</head>
<?php
include("conn.php");
if(isset($_POST['del']))
{
$mm = $_POST["selected"];
$id =implode(",",$mm);
$sql = "delete from forums where id in(".$id.")";
//echo $sql;
$result=mysql_query($sql);
echo $result?"刪除成功":"刪除失敗";
}
?>
<table style="BORDER-BOTTOM-WIDTH: 1px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=600 align=center border=1 bordercolor=#ddddff>
<tr align=middle>
<td height=40 bgColor=#DFFFDF colspan=3>論壇列表</td>
</tr>
<tr>
<td colspan=3><a href="login.php" style="float:right">[退出系統]</a><a href="add_forum.php" style="float:right">[添加論壇]</a></td>
<td></td>
</tr>
<tr align=middle>
<td height=40 bgColor=#DFFFDF width=80>狀態</td>
<td height=40 bgColor=#DFFFDF>論壇</td>
<td height=40 bgColor=#DFFFDF>最後更新</td>
</tr>
<?php
$sql="select * from forums";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if($num>0)
{
while($row=mysql_fetch_array($result)){
?>
<tr align=middle>
<td bgColor=#fffbec><input type="checkbox" name="selected" value="1"/></td>
<td height=50 bgColor=#fffbec width=300>
<?php
echo "<div><a href=\"forums.php?F=".$row['ID']."\">".$row['forum_name']."</a></div>";
echo cutstr($row['forum_description'],24);//最多顯示24個位元組,12個字,多餘部分用省略號代替
echo "……";
?>
</td>
<td height=50 bgColor=#fffbec><div><?php echo $row['last_post_time']."by".$row['last_post_author']?></div></td>

</tr>
<?php
}
}
else
{
echo "<tr bgColor=#fffbec><td colspan=3>對不起,論壇尚在創建中……</td></tr>";
}
?>
<tr>
<td colspan=3> <input type="checkbox" name="selected" value="1" onclick="All(this,'selected')"/>全選/不全選</td>
</tr>
<tr>
<td><input type="button" name="del" id="del" value="刪除選中項"/>
<?php

?>
</td>
</tr>
</table>
</html>

資料庫你就自己建,望採納~

⑵ php封裝一個用戶類,裡面有登錄注冊方法,這個要怎麼寫

第一步:login.php

//登陸方法
public function login(){
//如果用戶名和密碼為空,則返回登陸頁面
if(empty($_POST['username']) || empty($_POST['password'])){
$data['verifycode'] = rand(1000,9999);//生成一個四位數字的驗證碼
//將驗證碼放入session中,注意:參數是數組的格式
$this->session->set_userdata($data);
//注意:CI框架默認模板引擎解析的模板文件中變數不需要$符號
//$this->parser->parse("admin/login",$data);
//smarty模板變數賦值
$this->tp->assign("verifycode",$data['verifycode']);
//ci框架在模板文件中使用原生態的PHP語法輸出數據
//$this->load->view('login',$data);//登陸頁面,注意:參數2需要以數組的形式出現
//顯示smarty模板引擎設定的模板文件
$this->tp->display("admin/login.php");
}else{
$username = isset($_POST['username'])&&!empty($_POST['username'])?trim($_POST['username']):'';//用戶名
$password = isset($_POST['password'])&&!empty($_POST['password'])?trim($_POST['password']):'';//密碼
$verifycode = isset($_POST['verifycode'])&&!empty($_POST['verifycode'])?trim($_POST['verifycode']):'';//驗證碼

//做驗證碼的校驗
if($verifycode == $this->session->userdata('verifycode')){
//根據用戶名及密碼獲取用戶信息,注意:參數2是加密的密碼
$user_info=$this->user_model->check_user_login($username,md5($password));
if($user_info['user_id'] > 0){
//將用戶id、username、password放入cookie中
//第一種設置cookie的方式:採用php原生態的方法設置的cookie的值
//setcookie("user_id",$user_info['user_id'],86500);
//setcookie("username",$user_info['username'],86500);
//setcookie("password",$user_info['password'],86500);
//echo $_COOKIE['username'];

//第二種設置cookie的方式:通過CI框架的input類庫
$this->input->set_cookie("username",$user_info['username'],3600);
$this->input->set_cookie("password",$user_info['password'],3600);
$this->input->set_cookie("user_id",$user_info['user_id'],3600);
//echo $this->input->cookie("password");//適用於控制器
//echo $this->input->cookie("username");//適用於控制器
//echo $_COOKIE['username'];//在模型類中可以通過這種方式獲取cookie值
//echo $_COOKIE['password'];//在模型類中可以通過這種方式獲取cookie值

//第三種設置cookie的方式:通過CI框架的cookie_helper.php函數庫文件
//這種方式不是很靈驗,建議大家採取第二種方式即可
//set_cookie("username",$user_info['username'],3600);
//echo get_cookie("username");

//session登陸時使用:將用戶名和用戶id存入session中
//$data['username']=$user_info['username'];
//$data['user_id']=$user_info['user_id'];
//$this->session->set_userdata($data);

//跳轉到指定頁面
//注意:site_url()與base_url()的區別,前者帶index.php,後者不帶index.php
header("location:".site_url("index/index"));
}
}else{
//跳轉到登陸頁面
header("location:".site_url("common/login"));
}
}
}
}

第二步:User_model.php

//cookie登陸:檢測用戶是否登陸,如果cookie值失效,則返回false,如果cookie值未失效,則根據cookie中的用戶名和密碼從資料庫中獲取用戶信息,如果能獲取到用戶信息,則返回查詢到的用戶信息,如果沒有查詢到用戶信息,則返回0
public function is_login(){
//獲取cookie中的值
if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){
$user_info = false;
}else{
$user_info=$this->check_user_login($_COOKIE['username'],$_COOKIE['password']);
}
return $user_info;
}

//根據用戶名及加密密碼從資料庫中獲取用戶信息,如果能獲取到,則返回獲取到的用戶信息,否則返回false,注意:密碼為加密密碼
public function check_user_login($username,$password){
//這里大家要注意:$password為md5加密後的密碼
//$this->db->query("select * from ");
//快捷查詢類的使用:能為我們提供快速獲取數據的方法
//此數組為查詢條件
//注意:關聯數組
$arr=array(
'username'=>$username,//用戶名
'password'=>$password,//加密密碼
'status'=>1 //賬戶為開啟狀態
);
//在database.php文件中已經設置了數據表的前綴,所以此時數據表無需帶前綴
$query = $this->db->get_where("users",$arr);
//返回二維數組
//$data=$query->result_array();
//返回一維數組
$user_info=$query->row_array();
if(!empty($user_info)){
return $user_info;
}else{
return false;
}
}

第三步:其它控制器:

public function __construct(){

//調用父類的構造函數
parent::__construct();
$this->load->library('tp'); //smarty模板解析類
$this->load->helper('url'); //url函數庫文件
$this->load->model("user_model");//User_model模型類實例化對象
$this->cur_user=$this->user_model->is_login();
if($this->cur_user === false){
header("location:".site_url("common/login"));
}else{
//如果已經登陸,則重新設置cookie的有效期
$this->input->set_cookie("username",$this->cur_user['username'],3600);
$this->input->set_cookie("password",$this->cur_user['password'],3600);
$this->input->set_cookie("user_id",$this->cur_user['user_id'],3600);
}

$this->load->library('pagination');//分頁類庫
$this->load->model("role_model");//member_model模型類
$this->load->model("operation_model");//引用operation_model模型
$this->load->model("object_model");//引用object_model模型
$this->load->model("permission_model");//引用permission_model模型
}

⑶ 使用php實現用戶注冊和登錄功能製作 !急求大神幫助!

1、需要建立一個數據表test
id int(10) primary key not null increment
name char(4) not null
pass char(10) not null
age int(2)
city char(5)
2、html頁面自己寫,用表單post傳參
3、.php頁面,處理接收到的參數,於資料庫裡面的用戶名和密碼比對,若果正確,用Js框,輸出歡迎頁面,如果不多返回到登陸頁面
這里告訴你一個小技巧,很多時候我們都是拿用戶名和密碼一起比對,如果都正確則跳轉。這個地方其實我們需要防止sql注入攻擊,我們可以寫兩條語句,當用戶名正確,我們才執行下一條密碼比對語句,這樣可以有效防止sql的注入攻擊。

⑷ php 中注冊用戶之後 怎麼直接登錄

我用的是sso(可以網路,原理不說了)登錄模塊,流程是這樣的:
先注冊帳號,注冊成功後user表中有改用會的數據,然後生成一個otp(通行證,用來查詢注冊的用戶信息)和一個signmsg(md5加密驗證字元串),然後在注冊成功後直接用javascript提交的otp和signmsg隱藏表單信息,然後在login模塊用otp查詢到用戶信息
和驗證msgsign的可用性,都通過則登錄成功,否則登入異常。

⑸ php用戶登錄與注冊

你的$mysqli是什麼東西? new mysqli(..);的嗎?
那樣的話query方法在不出錯的情況下返回的是TRUE或 mysqli_result 對象,所以不會出else的結果
應該判斷$result->num_rows來判斷有沒有取到數據

⑹ php實現用戶注冊和登入,不用做效果求大牛指導

登陸界面 login.php
<form action="logincheck.php" method="post">
用戶名:<input type="text" name="user"/><br/>
密碼:<input type="password" name="pass"/><br/>
<input type="submit" name="sub" value="登陸"/>
<a href="register.php">注冊</a>
</form>
登陸處理界面logincheck.php
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query("set names 'gbk'");
$nsql="select username,passwd,nick from userinfo where username = '$_POST[user]' and passwd = '$_POST[pass]'";
$result = mysql_query($nsql);
$num = mysql_num_rows($result);
if($num){
$row = mysql_fetch_array($result);
echo "歡迎您,$row[2]";

}else{
echo"<script>alert('用戶名或密碼不正確');history.go(-1);</script>";
}
?>
注冊界面register.php
<form action="regcheck.php" method="post">
用戶名:<input type="text" name="user"/><br/>
密碼:<input type="password" name="pass"/><br/>
昵稱:<input type="text" name="nick"/><br/>
<input type="submit" name="sub" value="注冊"/>
</form>
注冊處理界面regcheck.php
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query("set names 'gbk'");
$nsql="select username from userinfo where username = '$_POST[user]'";
$result = mysql_query($nsql);
$num = mysql_num_rows($result);
if($num){
echo "<script>alert('用戶名已存在注冊失敗');history.go(-1);</script>";
}else{
$isql = "insert into userinfo values('$_POST[user]','$_POST[pass]','$_POST[nick]')";
mysql_query($isql);
echo"<script>alert('注冊成功');history.go(-1);</script>";
}
?>

⑺ 怎樣用php做一個網站的登錄注冊

在php文件中嵌入html代碼(包含表單,做好表單驗證),提交到一個表單處理文件(php文件),處理文件里查詢資料庫,和用戶提交的用戶名,密碼匹配,異常則登錄失敗,正常則登錄成功,跳轉頁面(重定向或轉發)。注冊同理做好html頁面,提交到注冊處理頁,先校驗資料庫是否存在用戶名,有則返回注冊頁,提示注冊失敗,無則在資料庫插入用戶注冊表單的信息。

閱讀全文

與php登陸注冊教程相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:755
蘋果郵件無法連接伺服器地址 瀏覽:960
phpffmpeg轉碼 瀏覽:669
長沙好玩的解壓項目 瀏覽:141
專屬學情分析報告是什麼app 瀏覽:562
php工程部署 瀏覽:831
android全屏透明 瀏覽:730
阿里雲伺服器已開通怎麼辦 瀏覽:801
光遇為什麼登錄時伺服器已滿 瀏覽:300
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:141
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:890
app轉賬是什麼 瀏覽:163