The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> PHP Login Troubles Using PDO
SBH
post Aug 11 2015, 05:05 AM
Post #1


Member
***

Group: Members
Posts: 82
Joined: 6-February 15
Member No.: 22,158



I searched & went through a few threads on PHP login scripts. Since I could not find a way out, I am posting my code & requesting for help, as I'm not sure why the login doesn't happen. Thanks.

'Login Failed' is the message I persistently get. You can see in the bottom if-else condition within the Try Loop

CODE

$usrname = filter_var($_POST['usrname'], FILTER_SANITIZE_STRING);
    $passwrd = filter_var($_POST['passwrd'], FILTER_SANITIZE_STRING);

    /*** now we can encrypt the password ***/
    $passwrd = sha1( $passwrd );
    
    /*** connect to database ***/
    /*** mysql hostname ***/
    $mysql_hostname = 'localhost';

    /*** mysql username ***/
    $mysql_username = ‘XXXXX’;

    /*** mysql password ***/
    $mysql_password = ‘YYYYY’;

    /*** database name ***/
    $mysql_dbname = ‘ZZZZZ’;

    try
    {
        $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname",
                $mysql_username, $mysql_password);
        /*** $message = a message saying we have connected ***/

        /*** set the error mode to excptions ***/
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        /*** prepare the select statement ***/
        $stmt = $dbh->prepare("SELECT username, password FROM U_Authentications
    *              WHERE username = :usrname AND password = :passwrd");

        /*** bind the parameters ***/
        $stmt->bindParam(':usrname', $usrname, PDO::PARAM_STR);
        $stmt->bindParam(':passwrd', $passwrd, PDO::PARAM_STR, 40);

        /*** execute the prepared statement ***/
        $stmt->execute();

        /*** check for a result ***/
        $user_id = $stmt->fetchColumn();

        /*** if we have no result then fail boat ***/
        if($user_id == false)
        {
                $message = 'Login Failed';
        }
        /*** if we do have a result, all is well ***/
        else
        {
                /*** set the session user_id variable ***/
                $_SESSION['user_id'] = $user_id;

                /*** tell the user we are logged in ***/
                $message = 'You are now logged in';
        }


    }
    
    catch(Exception $e)
    {
        /*** if we are here, something has gone wrong with the database ***/
        $message = 'We are unable to process your request. Please try again later"';
    }
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
CharlesEF
post Aug 12 2015, 06:03 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



In addition to what Christian said about the * character, I see some funny single quote characters also, like:
CODE
/*** mysql username ***/
$mysql_username = ‘XXXXX’;

/*** mysql password ***/
$mysql_password = ‘YYYYY’;

/*** database name ***/
$mysql_dbname = ‘ZZZZZ’;

Seems you have both types of single quotes spread around. You should change them to normal single quotes.

Also, your try block looks funny. A try/catch block catches an error (like connecting to your database) but you seem to be trying to catch all errors under the sun.

This post has been edited by CharlesEF: Aug 12 2015, 06:05 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
SBH
post Aug 12 2015, 06:06 AM
Post #3


Member
***

Group: Members
Posts: 82
Joined: 6-February 15
Member No.: 22,158



Ok lemme reply to both of you on by one.

First @Christian J - Yes, Login Failed is what I get, despite I using the username/password combo, which I've populated in the mysql table. As for the *, you can ignore. It's a copy paste mistake on my part.

@CharlesEF - The funny try block, as you call them, + quotes are stuff that I copy-pasted from a website as is. Will try to change that & see if that helps in any way.

This post has been edited by SBH: Aug 12 2015, 06:10 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic
SBH   PHP Login Troubles Using PDO   Aug 11 2015, 05:05 AM
SBH   Boy, over a day & no response. Wonder why? :( ...   Aug 12 2015, 01:58 AM
Christian J   'Login Failed' is the message I persisten...   Aug 12 2015, 05:38 AM
CharlesEF   In addition to what Christian said about the * cha...   Aug 12 2015, 06:03 AM
SBH   Ok lemme reply to both of you on by one. First @C...   Aug 12 2015, 06:06 AM
CharlesEF   @CharlesEF - The funny try block, as you call the...   Aug 12 2015, 06:13 AM
SBH   @CharlesEF - The funny try block, as you call th...   Aug 12 2015, 06:20 AM
CharlesEF   [quote name='CharlesEF' post='108038' date='Aug 1...   Aug 12 2015, 06:24 AM
SBH   [quote name='CharlesEF' post='108038' date='Aug ...   Aug 12 2015, 09:09 AM
Christian J   First @Christian J - Yes, Login Failed is what I ...   Aug 12 2015, 08:40 AM
SBH   First @Christian J - Yes, Login Failed is what I...   Aug 12 2015, 09:14 AM
masonh928   I use PDO, so lemme see what I can do…   Aug 13 2015, 08:32 PM
masonh928   Ok first off let me say, please do NOT use sha1. U...   Aug 13 2015, 08:38 PM
SBH   Ok first off let me say, please do NOT use sha1. ...   Aug 14 2015, 06:26 AM
masonh928   I think this may be better: $usrname = fil...   Aug 14 2015, 07:14 PM
masonh928   I may have missed something, if so please tell me....   Aug 14 2015, 07:16 PM
CharlesEF   I may have missed something, if so please tell me...   Aug 14 2015, 07:36 PM
masonh928   what do you mean? funny quotes?   Aug 14 2015, 10:12 PM
CharlesEF   what do you mean? funny quotes? I mean these vaia...   Aug 14 2015, 10:19 PM
SBH   Ok guys, am catching up on this thread now. First...   Aug 15 2015, 12:32 AM
CharlesEF   As for @CharlesEF, I've replaced the 'fun...   Aug 15 2015, 01:56 AM
SBH   There is nothing more to do. If you have changed...   Aug 15 2015, 05:40 AM
SBH   Am sure that I don't need to say this, but the...   Aug 15 2015, 12:34 AM
CharlesEF   Your current code displays several messages and 1 ...   Aug 15 2015, 09:35 AM
masonh928   This merits much change in my perspective will pos...   Aug 15 2015, 11:36 AM
masonh928   All I did was add bindValue(); I never edited ...   Aug 15 2015, 11:38 AM
CharlesEF   All I did was add bindValue(); I never edited ...   Aug 15 2015, 11:44 AM
masonh928   Charles are you saying my code was causing issues ...   Aug 15 2015, 11:40 AM
CharlesEF   Charles are you saying my code was causing issues...   Aug 15 2015, 11:45 AM
masonh928   Yeah, the OP should read this: bindValue vs bindP...   Aug 15 2015, 11:46 AM
masonh928   That's why I prefer bindValue() better…   Aug 15 2015, 11:48 AM
SBH   That's why I prefer bindValue() better… Ok...   Aug 15 2015, 12:07 PM
CharlesEF   [quote name='masonh928' post='108501' date='Aug 1...   Aug 15 2015, 12:18 PM
masonh928   This will be last post hopefully, just create a cl...   Aug 15 2015, 11:56 AM
masonh928   @Charles - I never even really payed much heed to ...   Aug 15 2015, 12:05 PM
masonh928   If you are referring to redirecting to main page a...   Aug 15 2015, 12:11 PM
SBH   @masonh928 - Ok lemme try again. Used the header r...   Aug 15 2015, 12:17 PM
masonh928   You are correct Charles was going to say exactly w...   Aug 15 2015, 12:20 PM
masonh928   this was it before you showed the whole code, mino...   Aug 15 2015, 12:23 PM
SBH   this was it before you showed the whole code, min...   Aug 15 2015, 12:50 PM
masonh928   Or just turn on error_reporting(E_ALL); haha   Aug 15 2015, 12:24 PM
CharlesEF   If you have any other problems just post it here. ...   Aug 15 2015, 01:06 PM
SBH   Sure will do, for now can I get help on the algori...   Aug 15 2015, 01:19 PM
CharlesEF   I already gave you my recommendation, phpass. Use...   Aug 15 2015, 01:36 PM
masonh928   yes if you don't have php 5.5 or the latter th...   Aug 15 2015, 01:39 PM
masonh928   You can ask away, we don't bite… lol   Aug 15 2015, 01:40 PM
SBH   You can ask away, we don't bite… lol :-) ...   Aug 15 2015, 03:31 PM
CharlesEF   It would be better if you attached the required pa...   Aug 15 2015, 03:46 PM
SBH   @CharlesEF - Will do mostly tomorrow.   Aug 15 2015, 03:49 PM
masonh928   ok when you do, post your example.   Aug 16 2015, 08:08 PM
SBH   ok when you do, post your example. Putting this ...   Aug 18 2015, 09:28 AM
masonh928   and E_STRICT   Sep 30 2015, 06:13 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th April 2024 - 01:04 PM