Will
Will

Reputation: 723

Determining the Source of $_POST

Is there an easy way to find out the source of a post variable in PHP?

Form on example.com/formone.php

<form method="post" action="test.php">
<input name="myusername"type="text">
<input name="mypassword" type="password">
<input type="submit">
</form>

Form on example.com/formtwo.php

<form method="post" action="test.php">
<input name="myusername"type="text">
<input name="mypassword" type="password">
<input type="submit">
</form>

I understand that I could use a hidden input, but I was wondering if PHP had a method to test the source of the POST.

Upvotes: 3

Views: 821

Answers (1)

jeffjenx
jeffjenx

Reputation: 17457

While you could potentially use the HTTP_REFERER server variable, it is not very reliable. Your best bet would be to use a hidden field.

Another alternative would be to switch out your submit input for a submit button. This way you can pass a value with it, retain the button's label, and test for that inside your test.php page:

<button name="submit" type="submit" value="form1">Submit</button>

In your PHP file you would then test:

if( $_POST['submit'] == "form1" )
    // do something

Upvotes: 8

Related Questions