3

I keep getting an undefined index to this..When I take error reporting off, all is good, but I am looking for a way to fix this the "RIGHT WAY"

On my form on register.php, the button looks like this

<input type="submit" name="submit" class="btn btn-kani btn-lg" value="Sign Up"/>

This is then linked to another page called login.php

Login.php code

I have tried

if (isset($_POST['submit']=="Sign Up")) {
// etc.

I have tried

if ($_POST['submit']=="Sign Up") { 
// etc.

Same with the logout because of the session, not sure how it should be coded....The button reads

<li class="inactive"><a href="logout.php?logout=1">Log Out</a></li>

and the code on login.php

if($_GET["logout"]==1 AND $_SESSION['id']) {
    session_destroy();
    header("Location:../logout.php");       
}

2 Answers 2

2

No isset() evaluates whats inside and tells you if its true or false:

So this expression doesn't make really sense on what you're trying to do:

if (isset($_POST['submit']=="Sign Up")) {

Should be like:

if(isset($_POST['submit'])) { // if index submit <input type="submit" name="submit" /> button was pressed
    // so if the button is pressed, then it will go inside this block
}

If you want to evaluate its presence and value you could do something like:

if(isset($_POST['submit']) && $_POST['submit'] == 'whatever value') {

}

Regarding the session you could check it like this:

if(isset($_GET['logout'], $_SESSION['id']) && $_GET['logout'] == 1) {
    // if both get logout and session id does exists and logout is equal to 1
    session_destroy();
    header('Location: ../logout.php');
}

If you want more supplemental info, you could check out deceze's The Definitive Guide To PHP's isset And empty. This is a good read and eloquently explains how to use it with test cases, similar to yours

Sign up to request clarification or add additional context in comments.

6 Comments

@KimNAnthonyStole what do you mean more than one? one variable? then add more inside the condition. if(isset(expression) && another one && another one || another one)
Since I can't post for a little more, maybe you can help. If there is an error on this form, what it will do is leave the info there so you dont have to type it in again, problem is it's getting undefined index again. <input type="text" name="name" class="form-control" value="<?php echo $_POST['name']; ?>" />
@KimNAnthonyStole if i can answer then yes, it check it
To sanitize it I use <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
@Ghost Thanks Ghost. Just as you, it's beneficial for all to give comprensive details as to what may be wrong with code, including suggestions and not just drop-in code. Many a time, you go the "extra mile" and that's a fine quality. Cheers
|
1

if ($_POST['submit']=="Sign Up") does not check first to make sure there's a value in $_POST['submit'], which can result in "undefined index";

if (isset($_POST['submit']=="Sign Up")) just doesn't even make sense. Try something like:

if (isset($_POST['submit']) && ($_POST['submit']==="Sign Up"))

3 Comments

YOU ARE THE MAN! What about the logout undefined index with the session involved? I will change the session to cookies later but this is just for starters.
Just apply the same logic to those other operations. if (isset(THING) && (THING == OTHER_THING))
if ((thing1) && (thing2) && (thing3) .... ) or you can nest them: if (thing1) { if (thing2) { ... }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.