首页 > 解决方案 > 登录后从数据库中获取用户名

问题描述

我正在尝试先登录用户,将他们重定向到管理面板,然后说“早安蒂姆!”。

现在问题来了:我使用会话登录用户,然后用户被重定向到管理面板,所以我需要使用用户名设置一个 cookie,然后在另一个站点上读取 cookie。

这里有一点代码:

** 登录网站 PHP **

$viewable = true;
require_once( './includes/login.php' );

if( isset( $_POST[/*button*/'login'] ) ) {
    if( !empty( $_POST['user'] ) && !empty( $_POST['password'] ) ){
        login( $_POST['user'], $_POST['password'], $loginsite );
    }
}else if ( isset( $_POST['register'] ) ) {
    if( !empty( $_POST['user'] ) && !empty( $_POST['password'] ) ){
        register( $_POST['Benutzer'], $_POST['Passwort'] );
    }
}

** MySQL PHP ** <?php

require_once('utils.php');

class Connection {

private const server = 'ip';

private const username = 'user';

private const password = 'pw';

private const logindb = 'database';

private $mysqli;

private $debug;

function __construct( $debug = false )
{
    $this->mysqli = new mysqli( self::server, self::username,self::password, 'database' );
    $this->connect();
    $this->createTableIfNoExists();

    $this->debug = $debug;
}

private function connect(){
    if ( $this->mysqli->connect_error ) die( "That didn't work! Contact us." .$this->mysqli- 
>connect_error );
}

private function createTableIfNoExists() {
    //creates database 
$logindb
    $sql = "CREATE TABLE IF NOT EXISTS " . self::logindb . "(
        id INT AUTO_INCREMENT,
        name VARCHAR(255) NOT NULL,
        password VARCHAR(1024) NOT NULL,
        salt VARCHAR(128) NOT NULL,
        PRIMARY KEY(id)
    );";
    $this->mysqli->query( $sql );
}

//debug
private function debug( $msg ){
    if( $this->debug ){
        debug_to_console( $msg );
    }
}

//testet ob die anmeldung erfolgreich war
public function mysql_checkLogin( $name, $pw ){
    $this->debug( 'Checking Login of ' . $name . ' :: ' . $pw );
    $stmt = $this->mysqli->prepare( "SELECT name, password, salt FROM " . self::logindb . " WHERE 
 name = ?" );
    $stmt->bind_param( "s", $name );
    $stmt->execute();
    $stmt->store_result();
    if( $stmt->num_rows === 0 )
    {
        $stmt->close();
        $this->debug( 'Could not login' );
        return false;
    }
    $stmt->bind_result( $uname, $password, $salt );
    $stmt->fetch();
    $stmt->close();
    $enc = md5( $pw . $salt );
    $this->debug( 'Testing Hash' );
    $this->debug( 'Salt is ' . $salt );
    $this->debug( 'From Database: ' . $password );
    $this->debug( 'Encrypted    : ' . $enc );
    if( $enc === $password  && $uname === $name ) return true;
    return false;
}

和管理面板:

<?php $erg = mysqli_query($db, "SELECT name FROM database");
            while($row = mysqli_fetch_object($erg)){
            $fullname=$row->name;
            $names = explode(" ",$fullname);
            echo $names[0];}

标签: phpmysqldatabaseauthentication

解决方案


我让它和饼干一起工作。

管理面板:

$cookie_name = "user";
$cookie_value = $uname;
setcookie($cookie_name, $cookie_value, time()+3600, "/", "domain.de/panel/", 1);


$fullname=$_COOKIE[$cookie_name];
$names = explode(" ",$fullname);
echo $names[0];

我实施了

password_hash()

感谢达曼!


推荐阅读