mardi 14 juin 2016

Connecting my php Login screen to my SQL DB

I don't really do much php myself but am in the process of creating a php login screen for a UI I am building to pull data to a table from a mysql db. I have the table connecting to the db fine and the general screen working but am having trouble with the login screen.

All I want achieve at the moment is for the login.php page to accept the correct credentials and direct the user to the index.php page.

The index.php page does contain the

include('session.php');    //include login session

For example I keep getting the following two errors

  1. Notice: Undefined index: active

  2. Fatal error: Call to undefined function session_register()

Here is the code for my login.php page

<?php

   include("config.php");
   session_start();
   $error=''; // Variable To Store Error Message

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT user_id FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql) or die("Error: ".mysqli_error($db));

      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         // Register $myusername, $mypassword and redirect to file "index.php"
         session_register("username");
         session_register("password");
         $_SESSION['login_user'] = $myusername;

         header("location: index.php");
      }else {
         $error = "Your Username Name or Password is invalid";
      }
   }
?>

Also here are the details for my config.php

$mysql_hostname = "localhost";  //your mysql host name
$mysql_user = "adminTest";          //your mysql user name
$mysql_password = "badgerBadger";           //your mysql password
$mysql_database = "mushroom";   //your mysql database

$db = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
//mysql_select_db($mysql_database, $bd) or die("Error on database connection");
  if($db->connect_errno > 0){
      die('Unable to connect to database [' . $db->connect_error . ']');
  }

Here is the code for my session.php file

<?php

   include('config.php');
   session_start();

   $user_check = $_SESSION['login_user'];
   $ses_sql = mysqli_query($db,"SELECT * FROM admin_tbl WHERE username = '$myusername' and password = '$mypassword'");
   $login_session =$row['first'];
   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['username'];

   if(!isset($_SESSION['login_user'])){
      header("location:login.php");
   }

?>

Any help would be greatly appreciated, thanks in advance :)

#

CHANGES

#

Updated code to remove typos relating to table queries and resolved some other issues that crept up after.

Upadated login.php code for correct calls

NEW ERRORS #######

  1. Undefined Variable username in session.php
  2. Undefined Variable password in session.php
  3. Undefined Variable row in session.php line 22

What's odd is that is parsing the variables and redirecting to the index.php page. What I have tried doing is setting the values for these variables to blank or $myusername " "; for example. This does get rid of the errors for the username and password, however it does still complain about line 24 in the session.php file = $login_session =$row['first']; complaining that it's an illegal field string, when it's actually a valid field.

TABLENAME admin_tbl ### Fields below

  • user_id
  • t_date
  • first
  • last
  • username
  • password
  • email
  • active

Aucun commentaire:

Enregistrer un commentaire