tags:

views:

44

answers:

2

Test to see if a POST has occurred. If so then verify the credentials; if correct then redirect to the destination page; if incorrect then display the error message on the form page. Then output the form.

A: 

In its most basic form:

if (!empty($_POST))

Other ways:

// check if one of the fields are present;
if (!empty($_POST['password']))

// attach a hidden field then
<input type="hidden" name="myHidden" value="yes">
if ($_POST['myHidden'] == 'yes')

// give a name and value to the submit button, then
<input type="submit" name="subBtton" value="Send">
if ($_POST['subButton'] == 'Send')
NullUserException
thanks it worked
tunetosuraj
This detects if there is data in the post array, it does not check what http method was used. This is wrong.
this is what I was looking for...thanks
tunetosuraj
@user257493 is it possible to have data in $_POST if the request method is GET?
captaintokyo
@user Huh... No, it seems like this is exactly what the OP wanted, as evidenced by the accepted answer.
NullUserException
You can put the data in otherwise, it's wrong. You can't test for a post by checking to see if there are variables, you check for a post by checking the http request.
@user "You can put the data in otherwise" Do enlighten us.
NullUserException
+1  A: 

Better do this in object oriënted way, I do it like this in my projects:

//check if user logged in
if(isset($_POST['sign_in'])):
    try
    {
        $userlogin = new User();
        $userlogin->loginusername = mysqli_real_escape_string($link, $_POST['username']);
        $userlogin->password = mysqli_real_escape_string($link, $_POST['password']);
        $userlogin->login();
    }
    catch(Exception $e)
    {
        header("Location: ".$siteurl."login.php?login=failed");
    }//end of catch exception
endif;//end of if user logged in

in the login() function in my user class I check if the user login is correct if not it will throw an exception (error) witch will be catched (see catch(exception...) ). I then redirect to another page but you could store the the error message in a variable like this:

$feedback = $e->getMessage();

and then just echo $feedback anywhere on your page

EDIT: this is my login function in my user class (just thought I would add it as well)

public function login()
    {
        global $link;
        $sql = mysqli_query($link, "SELECT userid, unique_id, username, password FROM user_extra WHERE username='".$this->loginusername."' AND password='".md5($this->password)."'");
        //there can only be one user with this username and password
        if(mysqli_num_rows($sql) == 1):
            $result = mysqli_fetch_object($sql);
            //sessions
            $_SESSION['userid'] = $result->userid;
            $_SESSION['username'] = $result->username;
            $_SESSION['unique_id'] = $result->unique_id;

            //cookies
            $expire=time()+60*60*24*30;
            setcookie("userid", $result->userid, $expire, "/");
            setcookie("password", $result->password, $expire, "/");
        else:
            throw new exception("Login failed");
        endif;
    }//end of function login 
krike