PHP+MySQL Login wont work -
i have been trying script go db , fetch data, login etc...
my php version 5.6. have included code in both index page [where form is] , problematic script in question.
login form code:
<form class="form" action="php-process/main_login-check.php" method="post"> <input type="text" placeholder="username" name='username' id='username' maxlength="50" > <input type="password" placeholder="password" name="password" id="password"> <button type="submit" id="login" name="login">go</button> <button type="reset" id="reg-button" onclick="mm_openbrwindow('app/register.php','register @ mysite.com','scrollbars=no,width=500,height=500');javascript:regprocess();"> register</button> </form>
processing script [main_login-check.php]
<?php /*** begin our session ***/ session_start(); /*** check if users logged in ***/ if(isset( $_session['id'] )) { $message = 'users logged in'; } /*** check both username, password have been submitted ***/ if(!isset( $_post['username'], $_post['password'])) { $message = 'please enter valid username , password'; } /*** check username correct length ***/ elseif (strlen( $_post['username']) > 20 || strlen($_post['username']) < 4) { $message = 'incorrect length username'; } /*** check password correct length ***/ elseif (strlen( $_post['password']) > 20 || strlen($_post['password']) < 4) { $message = 'incorrect length password'; } /*** check username has alpha numeric characters ***/ elseif (ctype_alnum($_post['username']) != true) { /*** if there no match ***/ $message = "username must alpha numeric"; } /*** check password has alpha numeric characters ***/ elseif (ctype_alnum($_post['password']) != true) { /*** if there no match ***/ $message = "password must alpha numeric"; } else { /*** if here data valid , can insert database ***/ $phpro_username = filter_var($_post['username'], filter_sanitize_string); $phpro_password = filter_var($_post['password'], filter_sanitize_string); /*** can encrypt password ***/ $phpro_password = sha1( $phpro_password ); /*** connect database ***/ /*** mysql hostname ***/ $mysql_hostname = 'localhost'; /*** mysql username ***/ $mysql_username = 'root'; /*** mysql password ***/ $mysql_password = 'thepass'; /*** database name ***/ $mysql_dbname = 'login_main'; try { $dbh = new pdo("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); /*** $message = message saying have connected ***/ /*** set error mode excptions ***/ $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); /*** prepare select statement ***/ $stmt = $dbh->prepare("select id, username, password user username = :username , password = :password"); /*** bind parameters ***/ $stmt->bindparam(':username', $phpro_username, pdo::param_str); $stmt->bindparam(':password', $phpro_password, pdo::param_str, 40); /*** execute prepared statement ***/ $stmt->execute(); /*** check result ***/ $user_id = $stmt->fetchcolumn(); /*** if have no result fail boat ***/ if($user_id == false) { $message = 'login failed'; } /*** if have result, ***/ else { /*** set session user_id variable ***/ $_session['id'] = $user_id; /*** tell user logged in ***/ $message = 'you logged in'; } } catch(exception $e) { /*** if here, has gone wrong database ***/ $message = 'we unable process request. please try again later'; } } ?> <html> <head> <title>login</title> </head> <body> <p><?php echo $message; ?> </body>
the error flagging working ok, , database connection ok. regardless of whether use existing record in database login or not, still error 'login failed'.
thanks in advance.
solved - input, appreciate helpful. problem simple, storing password in plain text, , asking in sha1, wasn't there. resolved encrypting password before sent database in registration form. cheers engin hint! :)
Comments
Post a Comment