Jump to content

Problem With Variables


dzhax

Recommended Posts

Im making a login script.I got it to submit and all and it works for now, but I keep getting this stupid error

Notice: Undefined index: username in C:\www\vhosts\localhost\files\login.php on line 2

<?PHP$toswitch = $_GET['username'];switch($toswitch){case 'admin':echo "Logged In...";break;default:include('loginform.php');break;}?>

I tried this

<?PHP$username='';$toswitch = $_GET['username'];switch($toswitch){case 'admin':echo "Logged In...";break;default:include('loginform.php');break;}?>

but then I get this error

Notice: Undefined index: username in C:\www\vhosts\localhost\files\login.php on line 3

Link to comment
Share on other sites

Do you get the error on a Form submit as well? The error means the variable is not yet declared. Do you have that code inside an if statement checking that the Form has been submitted?Shouldn't you be using the POST array from a Form input instead of GET?

Link to comment
Share on other sites

You are getting this notice because the script doesn't have that GET variable. This can happen when the form is submitted without such a field fielded out (assuming the form uses the GET method) or if you manually call the PHP file yourself without such a parameter.The solution is to check if the variable is set, and set it to something when it's not set by the user, like so:

$toswitch = isset($_GET['username']) ? $_GET['username'] : '';

(in this case, if the variable is set, we'll use it, and if not, we'll use an empty string instead)

Link to comment
Share on other sites

im using get function to bring the user for now and eventually a password from the loginform.php to this page.what i am trying to do is when the page loads in an iframe; if no one has logged in yet the login form will show. then when submitted, it changed to logged in...if you want to look at it, http://www.dzhax.com/test.php

Link to comment
Share on other sites

That's what boen is trying to explain to you. You're trying to access the value of $_GET['username'], but that variable isn't set. If the variable isn't set, $toswitch = $_GET['username'] generates a warning. Not being set is NOT the same as having an empty value. That's the way PHP works. You don't have to like it or understand it. You do have to prepare for it. That is what the isset() function is for.ALWAYS check if a $_POST or $_GET variable is set before you try to access it IN ANY WAY, and that includes assigning its value to another variable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...