欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 代码 100分,该如何解决

程序员文章站 2022-05-30 10:05:39
...
php 代码 100分
求一个用php做的注册和登录页面能提交到mysql的,端口是3307,数据库名是bbs,
表名是user-info,注册的是register.php 登录的是login.php
求代码,用来参考学习
------解决思路----------------------


if(!in_array($_POST['type'],array('login','reg'))){
echo -1;
exit();
}
if($_POST['type']=='login'){
$username=addslashes($_POST['username']);
$pwd=$_POST['pwd'];
$sql="SELECT * FROM test WHERE name='$username'";//test改为user-info
$db=new DB();
if($user_exists=$db->execute_dql($sql)){
if(md5($pwd)==$user_exists[0]['pwd']){
session_start();
$_SESSION['username']=$user_exists[0]['name'];
echo 1;
exit();
}
}else{
echo -1;
exit();
}
}elseif($_POST['type']=='reg'){
$username=addslashes($_POST['username']);
$pwd=md5($_POST['pwd']);
$sql_exists="SELECT * FROM test WHERE name='$username'";//test改为user-info
$db=new DB();
if($db->execute_dql($sql_exists)){//已存在该用户
echo -2;
exit();
}
$sql="INSERT INTO test(name,pwd) VALUES('$username','$pwd')";//test改为user-info
if($code=$db->execute_dml($sql)){
session_start();
$_SESSION['username']=stripslashes($username);
echo 1;
exit();
}else{
echo -1;
exit();
}
}

class DB{
private $conn;
private $host="localhost";//localhost:3307
private $user="root";
private $password="123456";
private $db="test";//bbs
private $res;

function __construct(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("连接数据库失败".mysql_error());
}
mysql_select_db($this->db,$this->conn);
mysql_query("SET NAMES utf8");
}
function execute_dql($sql){
$this->res=mysql_query($sql,$this->conn) or die(mysql_error());
$r=array();
while($row=mysql_fetch_assoc($this->res)){
$r[]=$row;
}
return $r;
}
function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0;//失败
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//成功
}else{
return 2;//没有影响到行数
}
}
}
function __destruct(){
if(!empty($this->res)){
mysql_free_result($this->res);
}
mysql_close($this->conn);
}
}

?>


exe.php


if(!in_array($_POST['type'],array('login','reg'))){
echo -1;
exit();
}
if($_POST['type']=='login'){
$username=addslashes($_POST['username']);
$pwd=$_POST['pwd'];
$sql="SELECT * FROM test WHERE name='$username'";//test改为user-info
$db=new DB();
if($user_exists=$db->execute_dql($sql)){
if(md5($pwd)==$user_exists[0]['pwd']){
session_start();
$_SESSION['username']=$user_exists[0]['name'];
echo 1;
exit();
}
}else{
echo -1;
exit();
}
}elseif($_POST['type']=='reg'){
$username=addslashes($_POST['username']);
$pwd=md5($_POST['pwd']);
$sql_exists="SELECT * FROM test WHERE name='$username'";//test改为user-info
$db=new DB();
if($db->execute_dql($sql_exists)){//已存在该用户
echo -2;
exit();
}
$sql="INSERT INTO test(name,pwd) VALUES('$username','$pwd')";//test改为user-info
if($code=$db->execute_dml($sql)){
session_start();
$_SESSION['username']=stripslashes($username);
echo 1;
exit();
}else{
echo -1;
exit();
}
}

class DB{
private $conn;
private $host="localhost";//localhost:3307
private $user="root";
private $password="123456";
private $db="test";//bbs
private $res;

function __construct(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("连接数据库失败".mysql_error());
}
mysql_select_db($this->db,$this->conn);
mysql_query("SET NAMES utf8");
}
function execute_dql($sql){
$this->res=mysql_query($sql,$this->conn) or die(mysql_error());
$r=array();
while($row=mysql_fetch_assoc($this->res)){
$r[]=$row;
}
return $r;
}
function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0;//失败
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//成功
}else{
return 2;//没有影响到行数
}
}
}
function __destruct(){
if(!empty($this->res)){
mysql_free_result($this->res);
}
mysql_close($this->conn);
}
}

?>

------解决思路----------------------
多看看就有的,加油楼主
php 代码 100分,该如何解决

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频