html - PHP - How to print errors on login form on index page? -


ok, question kind of hard word, i'll try best. have index.php page pop-up login form , have login.php login php stuff deal request. @ moment can seem print login errors on login.php page, opens blank white page errors on that, due fact form action on index.php form set "login.php" deals login stuff.. want print erros on login form in index.php, have login.php deal rest of php. hope kind of makes sense.

<form action="login.php" method="post">                 <label>email address</label>                 <input type="text" name="email"/>                 <br />                  <label>password</label>                 <input type="password" name="password"/>                 <br />                 <!--<?php                      if(empty($errors)===false){                         echo output_errors($errors);                         }                 ?>-->                 <div class="checkbox">                     <input id="remember" type="checkbox" />                     <label for="remember">keep me signed in</label>                 </div>                  <div class="action_btns">                     <div class="one_half last"><input type="submit" class="btn btn-blue" value="login"></div>                     <div class="one_half last"><a href="#" id="register_form" class="btn">sign up</a></div>                 </div>             </form> 

above code form on index.php page. commented out php in want print errors. below login.php

    <?php include 'init.php';   function sanitize($data){     return mysql_real_escape_string($data); }  function output_errors($errors){     return '<ul><li>'.implode('</li><li>', $errors).'</li></ul>'; }  //check if user exists function user_exists($email){         $email = sanitize($email);         return (mysql_result(mysql_query("select count(id) register email = '$email'"),0) == 1)? true : false; }  //check if user has activated account function user_activate($email){         $email = sanitize($email);         return (mysql_result(mysql_query("select count(id) register email = '$email' , active =1"),0) == 1)? true : false; } function user_id_from_email($email){     $email = sanitize($email);     return (mysql_result(mysql_query("select id register email = '$email'"),0,'id')); } function login($email,$password){     $user_id = user_id_from_email($email);     $email = sanitize($email);     $password = md5($password);      return (mysql_result(mysql_query("select count(id) register email = '$email' , password ='$password'"),0) == 1)? $user_id : false; }   if(empty($_post)=== false){     $email = $_post['email'];        $password = $_post['password']; }  if(empty($email)|| empty($password) === true){         $errors[] = "you must enter username , password";  } else if(user_exists($email) === false){     $errors[] = "email address not registered";   } else if(user_activate($email) === false){     $errors[] = "you haven't activated account yet";    } else{     $login = login($email, $password);     if($login === false){         $errors[] = "email/password incorrect";     } else {         $_session['user_id'] = $login;         header('location: index.php');         exit();     } } if(empty($errors)===false){     header('location: index.php');     echo output_errors($errors);     }  ?> 

this init.php below stores array.

    <?php session_start(); error_reporting(); require 'connection.php';  $errors = array();  ?> 

a tip before start never put variables not constant prepared statements, you're allowing user input directly sql statements.

but more point, if want receive errors, i'd suggest use basic die($db->error); want return error via database.

always remember $db->close(); @ end of working connection.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -