Jump to content

PHP URL Variable Issue


ashleysmithd
 Share

Recommended Posts

Hi, I'm trying to achieve a basic function within a login system, whereby a user enters a wrong username and/or password, is returned back to the login screen where a message flagging 'incorrect password' is shown. The login system works and upon input of an incorrect username and/or password the user is taken back to the login screen, and a URL variable is passed called '?pass=bad'. The issue I'm encountering is that the variable 'pass' does not exist in the URL by default (and I'd rather it didn't), therefore whenever the page checks to see if 'pass' equals 'bad' I get an 'Undefined Index' error, unless, the user has typed in a wrong username/password in which case the variable 'pass' exists and it has no problem handling it. What I think I need to do is tell PHP to ignore that variable if it does not exist. I've tried if statements but as they still look for a variable that does not exist, I still get an error. Can anyone offer any advice on the issue? Thanks in advance. Example of the code I'm using:

<?if ($_GET['pass'] != NULL) {$incorrectpwd = 1; } else {  $incorrectpwd = 0; } if ($incorrectpwd == 1) {echo "<b><font color=red>Incorrect password</b></font>"; } ?>

Edited by ashleysmithd
Link to comment
Share on other sites

Guest So Called

Something like this:

if (isset($_GET['pass'])) $pass = $_GET['pass'];if ($pass == 'bad') .....

Or post what code isn't working.

Link to comment
Share on other sites

Something like this:
if (isset($_GET['pass'])) $pass = $_GET['pass'];if ($pass == 'bad') .....

Or post what code isn't working.

Many thanks, works now. Used:
<?$incorrectpwd = NULL;if (isset($_GET['pass'])) {$incorrectpwd = $_GET['pass'];} if ($incorrectpwd != NULL) {echo "<b><font color=red>Incorrect password</b></font>"; }?>

Link to comment
Share on other sites

Guest So Called

Okay, but another thing you should learn is to not make your code overly complicated. For example, this code would do the same:

<?if (isset($_GET['pass'])	echo "<b><font color=red>Incorrect password</b></font>";?>

I see no reason to make it any more complicated unless you're using your argument for additional purposes.

Edited by So Called
Link to comment
Share on other sites

Okay, but another thing you should learn is to not make your code overly complicated. For example, this code would do the same:
<?if (isset($_GET['pass'])	echo "<b><font color=red>Incorrect password</b></font>";?>

I see no reason to make it any more complicated unless you're using your argument for additional purposes.

I tried a few simpler options but was still getting issues (if statements not working as expected), the simplified solution you posted works fine though. Thanks again.
Link to comment
Share on other sites

Guest So Called

You're welcome. Just remember, the least code that gets the job done is the best code. And, the easier it is to read your code the easier it is to debug and maintain.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...