當前位置:首頁 » 編程語言 » php實現注冊登陸

php實現注冊登陸

發布時間: 2024-10-25 19:22:56

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在登錄頁面中設置「注冊」按鈕,然後點擊後即進入注冊頁面啊在此先感謝了

用Form表單,如<form action="你要跳轉的頁面網址" name="form" method="post"/
<input type="submit" name="name" value="注冊"/>

></form>
這樣的話,點擊注冊按鈕就跳轉到你想要的網址那兒去了

⑶ 如何用php做出登陸注冊留言板

用php做出登陸注冊留言板:

<form id="form1" name="form1" method="post" action="<?php echo site_url()."/publish/user_message"?>">
<textarea rows="5" cols="50" name="huifu" <?php if($uere_name == "0"){echo "disabled";}?> >
<?php
if($uere_name == "0")
{echo "抱歉你還沒登錄不能進行留言";}
?>
</textarea>
<input class="wole" name="author" value="<?php echo $author;?>" /><!--接受方帖子作者-->
<input class="wole" name="news_id" value="<?php echo $news_idx;?>" /><!--文章id-->
<input type="submit" name="Submit"/>
</form>
<script language="javascript">
function updateinfo(){
if(<?php echo $uere_name;?> == 1){
document.form1.Submit.value = "留言";
document.form1.Submit.disabled = false;
}
else{
document.form1.Submit.value = "還未登錄";
document.form1.Submit.disabled = "disabled";
}
}
updateinfo();
</script>

回復帖子:

<p>這里是<?php echo $is;?>樓 用戶:<?php echo $sel->receiver_author;?> <br />留言內容:<?php echo $sel->content?>


<a onClick="showdiv('contentid<?php echo $is;?>','showtext<?php echo $is;?>')" href="javascript:void(0)">回復</a>
<div id="contentid<?php echo $is;?>" class="none">
<?php
$query = $this->db->query("select * from message where son_id ='$sel->id' order by id");//獲取指定父id的子回復
$revis = $query->result();
foreach($revis as $row){?>
<p><?php if($row->sender_author == $row->receiver_author){echo $row->sender_author;}
else{ echo $row->sender_author."回復了:".$row->receiver_author;}?>
內容是:<?php echo $row->content?></p>
<?php }?>
<form action="<?php echo site_url()."/publish/son_message"?>" method="post">
<input name="son_idx" class="wole" value="<?php echo $sel->id?>" />
<input name="receiver_author" class="wole" value="<?php echo $sel->receiver_author;?>" />
<input class="wole" name="news_id" value="<?php echo $news_idx;?>" /><!--文章id-->
<textarea rows="5" cols="50" name="huifux"></textarea>
<br><input type="submit" name="sub" value="回復"></form></div></p>
<script language="JavaScript" type="text/JavaScript">
<!--
function showdiv(targetid,objN){

var target=document.getElementById(targetid);
var clicktext=document.getElementById(objN)

if (target.style.display=="block"){
target.style.display="none";
clicktext.innerText="回復";

} else {
target.style.display="block";
clicktext.innerText='收起';
}
}
-->
</script>

效果圖:

⑷ PHP做一個用戶登錄頁面

index.html登錄頁面代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登錄示例</title>
</head>

<body>
<form id="forms" name="forms" method="post" action="loginchk.php">
用戶名:<input type="text" id="uname" name="uname" value=""/><br/>
密碼:<input type="password" id="upass" name="upass" value=""/><br/>
<input type="submit" id="loginbtn" value="立即登錄"/>
<input type="reset" id="resetbtn" value="重新填寫"/>
</form>
</body>
</html>

loginchk.php 的PHP程序代碼如下:
<?php
$uname=trim($_REQUEST["uname"]);
$upass=trim($_REQUEST["upass"]);
if($uname=="admin"&&$upass=="admin")
{
echo "登錄成功";
}
else
{
echo "登錄失敗,<a href='index.html'>重新登錄</a>";
}
?>

以上只是一個簡單示例,真正的開始,需要考到很多因素,比如說登錄前有效性檢查,加入登錄驗證碼,程序需要連接資料庫進行用戶匹配等。
希望對你有幫助 。
如果使用資料庫進行進行匹配的話,PHP程序可以這樣改進一下。
<?php
$uname=trim($_REQUEST["uname"]);
$upass=trim($_REQUEST["upass"]);

$con = mysql_connect("localhost","root","root");
mysql_select_db("dbname", $con);
$result = mysql_query("select * from sers where uname='$uname' and upass='$upass'");
$rs = mysql_fetch_array($result);
if($rs)
{
echo "登錄成功";
}
else
{
echo "登錄失敗,<a href='index.html'>重新登錄</a>";
}
?>
不過你需要連接到你自己的指定的資料庫和數據表。

熱點內容
java圖形開源框架 發布:2024-10-25 21:23:20 瀏覽:936
ubunturoot文件夾的文件 發布:2024-10-25 21:18:55 瀏覽:586
linux伺服器如何查看ip地址 發布:2024-10-25 21:18:39 瀏覽:634
何為編譯 發布:2024-10-25 21:16:58 瀏覽:826
什麼手機配置才高 發布:2024-10-25 21:16:09 瀏覽:748
安卓版托卡世界如何下載 發布:2024-10-25 20:51:24 瀏覽:578
賣二手安卓機哪個平台好 發布:2024-10-25 20:44:41 瀏覽:235
sql聯表更新 發布:2024-10-25 20:38:18 瀏覽:481
文件加密拒絕訪問 發布:2024-10-25 20:29:58 瀏覽:736
紅樓夢的編譯人有哪些 發布:2024-10-25 20:12:33 瀏覽:136