-2

My question = Why is there no value assigned to my $username when I try to use it in editor.php?

I have a log in system that works. Once the user logs in, I want to create a welcome line that says "Welcome [INSERT USERNAME HERE]". Currently, I am having issues with getting the echo to retrieve the variable "$username" since I get this error:

Notice: Undefined variable: username in editor.php on line 60.

Here is line 60 in editor.php. NOTE that editor.php is the page that the user is directed to once they have SUCCESSFULLY logged in

<h4>Welcome <?php  echo $username;?> , to the editor.</h4>

The $username variable is declared in the loginAuth.php which is what runs when the user submits his/her login details on login.php.

Here is the relevant code for loginAuth.php:

<?php
error_reporting(E_ALL);  
ini_set('display_errors', 1); 

//Connect to DB
include_once dirname(__FILE__).'/db_connect.php';
// PHP >= 5.3
include_once __DIR__.'/db_connect.php';



//starts session
session_start();
if (isset($_POST['loginSub'])) {
//Variables declaration
$username = $_POST['user'];
$password = $_POST['password'];

//if username doesnt not equal empty space and password does not equal empty  
space, then...
if(trim($username) != '' and trim($password) != ''){

//Sanitizes whatever is entered 
$username=stripslashes($username);
$password=stripslashes($password);

$username=strip_tags($_POST['user']);
$password=strip_tags($_POST['password']);


$username=mysqli_real_escape_string($conn,$username);
$password=mysqli_real_escape_string($conn,$password);

//SQL query 
$query = mysqli_query($conn, "SELECT * FROM users WHERE user='$username' AND 
password = '$password' ")or die(mysqli_error($conn));
//applies msqli_num_rows to the $query. Counts how many rows 
$numrows=mysqli_num_rows($query);
//if there is a row that matches what has been entered in form, then...
if($numrows > 0){

//initialize session
$_SESSION['login_user']= $username; 
//take them to editor.php   
header("location: editor.php"); // Redirecting To Other Page
exit;
}else{
     echo "Username or password is incorrect.";
    }
}else{  
    echo "Please enter information";
}
}
?>

So basically, the editor.php page cant obtain the $username var for some reason because apparently it has no value...Eventhough my login page successfully works.

As requested, my HTML FORM in login.php:

 <form id="login" method="POST" action="loginAuth.php">

                    <label for="user"><strong>Login:</strong></label>
                    <input type="text"size=20 autocorrect=off autocapitalize=words name="user">
                    <label for="password" > <strong>Password:</strong></label> 
                    <input type="password" name="password">

                    <input name="loginSub" type="submit" value="Login"> 
                </form>  

Here are other questions have I research but have failed to apply to my own question:

Reference - What does this error mean in PHP?

PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

Community
  • 1
  • 1
Kent Godfrey
  • 83
  • 1
  • 3
  • 12
  • you need to post your HTML form. You're most likely trying to echo that in the same file before it's defined. – Funk Forty Niner May 18 '16 at 21:52
  • 1
    when in doubt, use a conditional statement for it, or a ternary – Funk Forty Niner May 18 '16 at 21:54
  • 2
    or that you didn't start the session in the other file and not checking if the session is set, that's what I'm thinking at this point and is a simple fix really. if it's in 2 files, that's the problem. Unclear question, sorry. – Funk Forty Niner May 18 '16 at 21:55
  • session_start(); has been declared in both loginAuth.php and editor.php. That was my first concern. I also tried if(isset($_SESSION)['login_user']; Or something on the lines of that in my editor.php and it essentially broke my login system. – Kent Godfrey May 18 '16 at 21:59
  • 1
    See the answers below. Nothing more that I can do here or add. – Funk Forty Niner May 18 '16 at 21:59
  • 1
    `if (isset($_POST['loginSub'])) {` only declares your local on the form request. If you don't refetch it from the session afterwards, it remains unset. Also it seems you're operating entirely in global scope. While not a problem per se; that can become messy and is perhaps how you misunderstood why it isn't there. – mario May 18 '16 at 22:01

2 Answers2

4

Your variable doesn't exists in editor.php

You need to get from your $_SESSION like this:

editor.php

<?php
$username = $_SESSION['login_user'];
....
<h4>Welcome <?php  echo $username;?> , to the editor.</h4>
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • 1
    and there you have it, exactly what I meant up there, in so many words @KentGodfrey had you shown us your full code for all files... ;-) – Funk Forty Niner May 18 '16 at 22:03
  • I get put off spamming all my code since it becomes hard to read. I will look into improving future questions with this advice. Thanks – Kent Godfrey May 18 '16 at 22:07
1

Individual .php files are completely independent from each other when the user accesses them directly, as they do when you redirect them.

Since you redirect the user to editor.php, the variable $username that you set in login does not exist. It is a different process handling the request.

editor.php needs to handle the user login in some form, you may want to look at PHP Session Handling.

//initialize session
$_SESSION['login_user']= $username; 

You're storing the username in a session variable, here; one option is to mirror this in editor.php by doing...

$username = $_SESSION['login_user'];
Tordek
  • 10,628
  • 3
  • 36
  • 67