PHP Mysql Error? -
i want log in user on website php
. below alert not working:
else { echo 'you must enter pass'; }
the below code works when required fields empty, if password correct or not, alert not working.
<?php require 'core.inc.php'; if(isset($_post['email1']) && isset($_post['sifre1'])){ $username=$_post['email1']; $password=$_post['sifre1']; if(!empty($username) && !empty($password)){ $query="select e-mail,sifre kullanıcı e-mail='$username' , sifre='$password'"; echo mysql_error(); if($query_run=mysql_query($query)){ echo 'invalid13'; $query_num_rows = mysql_num_rows($query_run); if($query_num_rows==0) { echo 'invalid'; } else { echo 'ok'; } } } else { echo 'you must enter pass'; } } ?>
part of problem comes lack of separation of concerns.
lets see basic algorithm:
- check needed info log in user there.
- if is, query database user , password.
- if match, return success
- otherwise return failure: invalid
- if information missing, return failure: missing info
- if is, query database user , password.
let's try , implement in code.
require 'core.inc.php';
//takes in $_post object , returns string function log_in_user($post_object) {//note using global variables problematic, lets pass param $username = null; $password = null; //one @ time lets needed info if (isset($post_object['email1']) && !empty($post_object['email1'])) { $username = $post_object['email1'] } if (isset($post_object['sifre1']) && !empty($post_object['sifre1'])) { $password = $post_object['sifre1'] } //handle case don't have correct info if (is_null($username)) { return "you must enter username." } if (is_null($password){ return "you must enter password." } //if function hasn't returned point, validate credentials. return validate_credentials($username, $password);//pass through result } //put in separate function cleanliness , can handle //the changes need make how access db w/o affecting rest function validate_credentials($username, password) { $query="select e-mail,sifre kullanıcı e-mail='$username' , sifre='$password'"; //get connection database. details below change. //db_username , db_password credentials database, not user. $db = new pdo('mysql:host=localhost;dbname=testdb;charset=utf8', 'db_username', 'db_password'); try { $stmt = $db->query($query);//sets query $results = $stmt->fetchall(pdo::fetch_assoc); if (count($results) == 1) { return "ok"; } else { return "invalid credentials"; } } catch(pdoexception $ex) { return "an error occurred trying reach database. try again later."; } } //now execute login function echo log_in_user($_post);
so, can see, breaking code functions allow clarify issues you're seeing. first function validates inputs: initial problem 1 of validation. did user send through password? second function handles concern of validating set of credentials against database. second function can test different cases:
validate_credentials("good_username","awesome_password");//should exist in db work. return "ok" validate_credentials("bad_username","terrible_password");//should not exist in db. return "invalid credentials"
caveat: none of code tested, it's merely example of how might this.
Comments
Post a Comment