Home > php教程 > PHP源码 > php 账号互联

php 账号互联

PHP中文网
Release: 2016-05-23 16:38:57
Original
1439 people have browsed it

很简单的账号互联,留给需要的人吧。     

<?php
/*
本页面做的匆忙(2015年9月23日19:45:30),注意修改一些代码再使用,避免出现安全隐患。
使用方法,向本页POST数据,分别为sign(用户名或邮箱)和password
本页会返回Json,成功登陆会返回一些用户信息,code每个状态会不同
code为0时 POST数据不完整
code为1时 账户不存在
code为2时 用户名或密码错误
code为3时 成功登陆,并附带相关用户信息
 */
@$sign=addslashes($_POST[&#39;sign&#39;]);
@$password=addslashes($_POST[&#39;password&#39;]);
$status=array();
if($sign==null||$password==null){
    $status[&#39;code&#39;]=0;
    echo json_encode($status);
    exit();
}
// =====相关配置======
$db_server=&#39;localhost&#39;; //Mysql地址
$db_name=&#39;db&#39;; //Mysql数据库名
$db_user=&#39;root&#39;; //Mysql用户名
$db_password=&#39;&#39;; //MySQL密码
$db_port=3306;//Mysql端口,通常不需要修改
$login_table=&#39;user&#39;; //用户表表名
$login_sign=&#39;name&#39;; //用户表用户名或邮箱字段 意思就是用户名登陆或者邮箱登陆
$login_password=&#39;password&#39;; //用户表密码字段
//另外58行还有一个密码加密方式需要自定义下
class mysql extends PDO{
    public $server;
    public $database;
    public $user;
    public $password;
    public $sql;
    public function __construct($server,$database,$user,$password,$port=3306){
        $this->server = $server;
        $this->database = $database;
        $this->user = $user;
        $this->password = $password;
        parent::__construct("mysql:host=$server;port=$port;dbname=$database",$user,$password);
        $this->query(&#39;SET NAMES utf8&#39;);
    }
    public function fetchall($sql){
        $return = array();
        $re = $this->query($sql);
        while($row = $re->fetch(PDO::FETCH_ASSOC)){
            array_push($return,$row);
        }
        return $return;
    }
}
$DB= new mysql($db_server,$db_name,$db_user,$db_password,$db_port);
$res=$DB->fetchall(&#39;select * from &#39;.$login_table.&#39; where &#39;.$login_sign.&#39;="&#39;.$sign.&#39;"&#39;);
if(count($res)==0){
    $status[&#39;code&#39;]=1;
    echo json_encode($status);
    exit();
}
$encode_pwd=md5($password); //这里修改你密码的加密方式
if($encode_pwd!=$res[0][$login_password]){
    $status[&#39;code&#39;]=2;
    echo json_encode($status);
    exit();
}
unset($res[0][$login_password]);
$res[0][&#39;code&#39;]==3;
echo json_encode($res);
Copy after login

                                   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template