php写一个登陆注册代码,求大神解答。 200
数据库服务器:localhost;用户名:root;密码:123456;数据库:bbs。用户表包括:自增id(id),用户账号(usercode),密码(pwd),性别(...
数据库服务器:localhost;用户名:root;密码:123456;数据库:bbs。
用户表包括:自增id(id),用户账号(usercode),密码(pwd),性别(sex),角色(role 0,1,2分别代表不同权限),E-mail地址(email),昵称(nickname),头像(image)。 展开
用户表包括:自增id(id),用户账号(usercode),密码(pwd),性别(sex),角色(role 0,1,2分别代表不同权限),E-mail地址(email),昵称(nickname),头像(image)。 展开
4个回答
展开全部
index.html 包含注册和登录
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
.x-stage {
margin:50px auto auto;
width:400px;
font-size:12px;
}
.x-head{
font-size:0;
}
.x-tag{
display:inline-block;
vertical-align:middle;
font-size:12px;
font-weight:bold;
color:white;
background-color:gray;
height:30px;
line-height:30px;
text-align:center;
width:200px;
cursor:default;
}
.x-tag.x-active{
background-color:orange;
}
.x-body{
padding:15px 0 0 60px;
}
.x-panel{
display:none;
}
.x-panel.x-active{
display:block;
}
.x-panel label{
display:block;
line-height:30px;
}
.x-panel input{
vertical-align:middle;
}
</style>
<script>
window.onload = function(){
var tags = document.getElementsByClassName('x-tag');
var panels = document.getElementsByClassName('x-panel');
var head = document.getElementsByClassName('x-head')[0];
var lastIdx = 0;
var idx = /(?:^\?|&)idx(?:\=)(.*?)(?=&|$)/.exec(location.search);
var msg = /(?:^\?|&)msg(?:\=)(.*?)(?=&|$)/.exec(location.search);
idx = idx && idx[1] || 0;
msg = msg && msg[1] || '';
var active = function(idx){
if(idx == lastIdx)
return;
tags[lastIdx].className = tags[lastIdx].className.replace(/\sx-active/,'');
panels[lastIdx].className = panels[lastIdx].className.replace(/\sx-active/,'');
tags[idx].className += ' x-active';
panels[idx].className += ' x-active';
lastIdx = idx;
};
head.onclick = function(e){
var el = e.target;
if(!/x-tag/.test(el.className)) return;
for(var i=0,len=tags.length;i<len;i++){
if(el === tags[i]){
active(i);
break;
}
}
};
active(idx);
if(msg) alert(decodeURI(msg));
};
</script>
</head>
<body>
<div class="x-stage">
<div class="x-head">
<div class="x-tag x-active">登录</div>
<div class="x-tag">注册</div>
</div>
<div class="x-body">
<div class="x-panel x-active">
<form name ="login" action="response.php" method="post">
<label>帐号:</label>
<input type="text" name="usercode" required>
<label>密码:</label>
<input type="password" name="pwd" required>
<input type="submit">
<input type="hidden" name="req-type" value="login">
</form>
</div>
<div class="x-panel">
<form name = "regist" action="response.php" method="post">
<label>帐号:</label>
<input type="text" name="usercode" required>
<label>密码:</label>
<input type="password" name="pwd" required>
<label>性别:</label>
<input type="radio" name="sex" value="0" checked>男</input>
<input type="radio" name="sex" value="1">女</input>
<label>角色:</label>
<input type="checkbox" name="role" value="0" checked>角色1</input>
<input type="checkbox" name="role" value="1">角色2</input>
<input type="checkbox" name="role" value="2">角色3</input>
<label>E-Mail:</label>
<input type="text" name="email" required>
<label>昵称:</label>
<input type="text" name="nickname" required>
<label>头像:</label>
<input type="text" name="image" required>
<input type="submit">
<input type="hidden" name="req-type" value="regist">
</form>
</div>
</div>
</div>
</body>
</html>
response.php 包含注册和登录
<?php
$dsn = array('127.0.0.1', 'root', '123456', 'bbs',3306);
// 这里假设表名为user描述里没有说
$query_sql = 'select `usercode`,`id` from `user` where `usercode` = ? and `pwd` = ?';
$query_param_types = 'ss';
// 这里假设 sex role 是int类型 image是连接地址 也就是varchar类型
$insert_sql = 'insert into `user`(`usercode`,`pwd`,`sex`,`role`,`email`,`nickname`,`image`) values(?,?,?,?,?,?,?)';
$insert_param_types ='ssddsss';
function get_request(){
$params = array();
$type;
$i = 0;
foreach($_REQUEST as $key=>$param)
if($key!='req-type')
$params[$i++] = $param;
else $type = $param;
return array(
'params'=>$params,
'type'=>$type
);
}
function query($dsn,$sql,$param_types,$params,$fetch = true,$char_set='UTF8'){
$client = mysqli_init();
call_user_func_array(array($client,'real_connect'),$dsn);
if($char_set) $client->set_charset($char_set);
$stmt = $client->prepare($sql);
foreach($params as $pk=>$pv)
$params[$pk] = &$params[$pk];
array_unshift($params,$param_types);
$c_stmt = new ReflectionClass('mysqli_stmt');
$cm_stmt = $c_stmt->getMethod('bind_param');
$cm_stmt->invokeArgs($stmt,$params);
$stmt->execute();
if(!$fetch){
$affected_rows = $stmt->affected_rows;
$client->close();
return $affected_rows;
}
$result = $stmt->get_result();
$set = array();
while($row = $result->fetch_array(MYSQL_ASSOC))
$set[]=$row;
$result->free();
$client->close();
return $set;
}
$request = get_request();
$msg = '请求类别错误!';
$idx = 0;
$result = null;
if($request['type'] == 'login'){
$idx = 0;
$result = query($dsn,$query_sql,$query_param_types,$request['params']);
$msg = empty($result) ? '用户名或密码错误!' : '登录成功!';
}
if($request['type'] == 'regist'){
$idx =1;
$result = query($dsn,$insert_sql,$insert_param_types,$request['params'],false);
if(empty($result)){
$msg = '注册失败!';
$idx = 1;
}else {
$msg = '注册成功!';
$idx = 0;
}
}
header('location:index.html?msg='.$msg.'&idx='.$idx);
展开全部
建立数据库连接
获取前端提交过来的账号,查看该账号是否注册过
确定账号未注册过后把前端传过来其它信息写入数据库
搞定!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
稍等,我先站位!马上给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询