简体   繁体   English

使用SqlSrv在PHP中登录页面

[英]Login Page in Php with SqlSrv

I have a problem, I create a login page for DashBoard (with SqlSrv in database) When the password in the DB and the password typed by the user are the same I can not even connect to when. 我有一个问题,我为DashBoard创建了一个登录页面(数据库中带有SqlSrv),当数据库中的密码与用户键入的密码相同时,我什至无法连接。 This is my code : 这是我的代码:

<?php    
session_start();

    $serverName = "A106MVP19\SQLEXPRESS";   
    $uid = "sa";     
    $pwd = "root";    
    $databaseName = "FormaSport";   

    $connectionInfo = array( "UID"=>$uid,                              
                         "PWD"=>$pwd,                              
                         "Database"=>$databaseName);   

    /* Connect using SQL Server Authentication. */    
    $conn = sqlsrv_connect( $serverName, $connectionInfo);  
    if( $conn === false ) 
    {
     die( print_r( sqlsrv_errors(), true));
    }   


if(isset($_POST) && !empty($_POST['emailCo']) && !empty($_POST['mot_de_passeCo'])) 
    {   
    $login = $_POST["emailCo"];
    $pass  = $_POST["mot_de_passeCo"];
    extract($_POST);

    $sql = "SELECT pass FROM Formateur WHERE mail='".$login."'";
    $req =  sqlsrv_query($conn, $sql) or die('Erreur SQL!<br>'.$sql.'<br>');


    $data = sqlsrv_fetch_array($req, SQLSRV_FETCH_ASSOC);

echo $data['pass']."<br />";    //for view the pass for the user in database
echo $pass."<br />";   //for view the input pass
echo gettype($data['pass'])."<br />";   // type of pass in DB
echo gettype($pass)."<br />";  // type pass input

    if($pass !== $data['pass'])
        {
        echo '<p>Mauvais login / password. Merci de recommencer</p>';
        include('index.html'); // On inclut le formulaire d'identification en dessous du message d'information
        exit;
        }
    else
        {
        $_SESSION['login'] = $login;
        echo 'Vous etes bien logué';
        // Mettre une redirection ici 
        // redirection here
        }    
    }   
    else {
    echo '<p>Vous avez oublié de remplir un champ.</p>';
    include('index.html'); // On inclut le formulaire d'identification
    exit;
}


?>

How you can view here : 您如何在这里查看:

echo $data['pass']."<br />";    //for view the pass for the user in database
echo $pass."<br />";   //for view the input pass
echo gettype($data['pass'])."<br />";   // type of pass in DB
echo gettype($pass)."<br />";  // type pass input

I have test the type of password and they are the same. 我已经测试了密码的类型,它们是相同的。

If everyone view where I have make an error, I need your help ! 如果每个人都认为我出错了,我需要您的帮助!

Thank a lot. 非常感谢。 Pokky. Pokky。

PS : (Sorry for the "frenglish") PS :(对“法国人”表示抱歉)

You must put your query into a PHP loop, so the search will be executed in the database. 您必须将查询放入PHP循环中,以便搜索将在数据库中执行。

$sql = "SELECT pass FROM Formateur WHERE mail='".$login."'";
$req =  sqlsrv_query($conn, $sql) or die(print_r(sqlsrv_errors(),true));

if(sqlsrv_has_rows($req) != 1){
    echo "* Error! no result found OR more than one result found";
}else{
    while($data = sqlsrv_fetch_array($req, SQLSRV_FETCH_ASSOC)){
        $_SESSION['id'] = $data[0];
        $_SESSION['login'] = $data[1];
    }
}

Also, I recommend checking the password in the database as well and using PDO class for authentication, but in this case, you have to check if the extension for PDO is enabled in your PHP configuration in the server. 另外,我建议也检查数据库中的密码,并使用PDO类进行身份验证,但是在这种情况下,您必须检查服务器中的PHP配置中是否启用了PDO扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM