Jump to content

SECURITY: PHP Form Security, Best way?


stewart715

Recommended Posts

What's the best way to secure a form that has action through a php file?

Example:
[code]<form action="/process.php" method="POST">
<select name="color">
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>
</select>
<input type="submit" />
</form>[/code]

What could I place in process.php so the above script cannot just be placed in a remote HTML file and submitted?

Thanks for all of your help!
Link to comment
Share on other sites

[quote author=stewart715 link=topic=113416.msg460894#msg460894 date=1162345152]
The form is included in an index.php file somehow, can I change $url to like http://mysite.com/ or something like that?
[/quote]

Change it to this:

[code]<?php

  $url = $_SERVER[REQUEST_URI];

  if($url != '/index.php') {

  header("Location: error.php");

  }

?>[/code]
Link to comment
Share on other sites

[quote author=stewart715 link=topic=113416.msg460899#msg460899 date=1162345386]
Hmm that's weird..still not working

well let me tell you that the form is located at

http://mydomain.com/?q=privacy
[/quote]

That changes things.  ;)

Then just do this:

[CODE]<?php

  $url = $_SERVER[REQUEST_URI];

  if($url != '/index.php?q=privacy') {

  header("Location: error.php");

  }

?>
[/CODE]
Link to comment
Share on other sites

What page are you posting that code in? And if your are submitting the form from index.php to process.php...then I will address the last two questions you had. First, add the following code in "[color=red]process.php[/color]":

[code]<?php

  $url = $_SERVER[HTTP_REFERER];

  preg_match('/http:\/\/yourdomain.com\/index.php\?q=([a-z0-9]+)/',$url,$q);

  if($url != "http://yourdomain.com/index.php?q=$q[1]") {

  header("Location: error.php");
  }

?>[/code]

You mentioned you would like it to work regardless of what the value of "q" was. The above code also addresses that.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.