本文目录一览:
求一用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写一个登陆注册代码,求大神解答。
index.html 包含注册和登录
!doctype html
html lang="en"
head
meta charset="UTF-8"
titleindex/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;ilen;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
labelE-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);
求一个简单的PHP注册,登陆代码
我帮你找了个小程序
程序介绍:
1、共4个页面,conn.php连接数据库、img.php图片验证码、index.php登录页面、register.php注册页面
2、注册页面全是用js来验证的,所以不太完善,后续会改进
3、还没有学习ajax,所以图片没法点击刷新。原谅我吧
4、每段代码都含有详细注释,方便交流学习
程序使用:
1、下载源码上传到你网站某个目录
2、打开你的数据库,在某个表中执行readme.txt中的SQL语句创建字段用来存放用户数据
3、修改conn.php填写对应的数据库地址、用户名、密码、数据表
4、确保上述操作无误后,打卡URL地址进行测试
源码git地址
php注册功能代码
注册页面表单提交时没有提交sex字段,$_SESSION['sname']没有赋值,可以直接在前面加上error_reporting(E_ALL^E_NOTICE),防止NOTICE报出