Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

PHP Submission

Hi Experts,

Is it possible when a form is submitted to a PHP page, that PHP page to check the absolute path of the form page?

In other words, I have just noticed some invalid data I have been validating with JavaScript, and found out that someone has view source, saved it on the local machine, tampered with the JavaScript and adjusted the form action to the PHP's absolute URL.

So, if I can verify the absolute URL of the page being submitted (without using hidden fields, of course), then I can avoid this.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of APD Toronto

ASKER

So, you're saying using a PHP session variable, that way no one can change this...?
Yep, it's only stored at the server and as long as you are not echoing it out to the browser it cannot be known by the user.
What if ...

1- The HTML and JavaScript is downloaded on a local machine

2- User goes to the real form on the server, the server creates the session variable

3- User changes URL of same window to point to the local version of their machine, with the adjusted code.

4- User clicks Submit from their local machine that goes to the PHP page on the server. Wouldn't the PHP page pickup the session variable from step 2?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
http://en.wikipedia.org/wiki/Cross-site_request_forgery#Prevention

No one system is going to be 100% foolproof but session is pretty reliable as it only exists while on your site.
To test, I just wrote the following 2 pages.

How come on test2.php $currSess always returns '' (empty string)? I realize that in test.php I'm running session_start(), but shouldn't that be carried over to all pages thereafter until session_end() is executes?

//test.php:

<?php
    session_start();
    $_SESSION['sess'] = session_id();
?>

<form method="post" action="test2.php">
    
    <input type="text" name="txtSess" value="<?php echo $_SESSION['sess']; ?>">
    
    <input type="submit">
    
</form>

//test2.php:

<?php
    
    $currSess = session_id();
    $reqSess = $_REQUEST['txtSess'];
    
    echo 'curr = ' . $currSess . ' <br> '
        . 'req = ' . $reqSess . ' <br><br>';
    
    if ($currSess == $reqSess) 
    {
        echo 'session is VALID';
    }
 else 
    {
        echo 'session is not VALID';
    }

?>

Open in new window

session_start() is needed on all pages where you are using sessions and before the code
but wouldn't that keep generating new sessions?
No, session are per user per visit (unless the session times out which isn't a worry for you anyway)

http://php.net/manual/en/function.session-start.php
No, session_start() checks to see if a valid session exists already.  If it does, then it continues the current session and you can access any $_SESSION variables you have set.  It only starts a new session if it does not find an existing one.  This is probably one of the most common misunderstandings about PHP sessions.
Using the code above, with session_start() in test2.php, I was able to bypass the code using the scenario within ID: 40337055 above.

So, I'll look at the ASP Code in my other post, with the assumption that there's something equivalent in PHP. Otherwise, I'm open to other suggestions.
" bypass the code"?  What does that mean?  Is that what you want or not?
If you refer to my original post, I noticed my JavaScript is being bypassed.  I need to prevent this.
That can't happen because the session cookie is only stored at the original url, if you post from another site (or even locally thru localhost) then there is no session cookie.
When I went through my 4-step procedure above, using the code, I got "session is VALID"
No you're right (put my logic head on - what was that tv program where he used to change his head...)
Like we are all saying there is no 100% foolproof way - you can implement all the different things to check like referer and sessions but it won't deter the hardened hacker and that is why you need to cleanse any data sent through a form.
Is there a php equivalent to HTTP_REFERRER and Remote IP
You can not prevent your javascript from being bypassed.  You can Not.  Spammers and hackers scan websites every day looking for vulnerable forms and post directly to the action pages which bypasses everything on the form page.

Javascript checking and validation on the form page should be there to help the user fill out the form correctly.  PHP checking is to prevent the spammers and hackers from posting nonsense or breaking into your site and databases.
Here are a pair of simple PHP pages that use the things we've talked about.  Adjust the URLs and file names to suit your situation.
PHPsess01.php
<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

// Create a unique session ID to use with this page
session_start(); 

$domain = $_SERVER['REMOTE_ADDR'];

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Sessions check</title>
</head>
<body>
<h1>PHP Sessions check</h1>
<form action="PHPsess02.php" method="post">
<input type="text" name="innie1" value="innie1" />&nbsp;
<input type="text" name="outie2" value="outie2" />&nbsp;
<input type="submit" value="Submit" />
</form>

</body>
</html>

Open in new window

PHPsess02.php
<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

// check referrer, if no referrer, exit because it is a direct post
// Note that the refferrer URL is a complete absolute URL like http://www.yoursite.com/yourpage.php
// That's why I'm splitting it up to check it.  It could also be 'https://'.
if(isset($_SERVER['HTTP_REFERER'])) {
	$refchk = $_SERVER['HTTP_REFERER'];
	$refchka = explode("//",$refchk);
	if($refchka[1] != "10.202.46.40/ee2/PHPsessions/PHPsess01.php") exit;
	}
//else exit without creating a session;

// Create a unique session ID to use with this page
session_start(); 
$sessid = session_id();

$domain = $_SERVER['REMOTE_ADDR'];

if (!isset($_POST['innie1']))  $innie1 = ''; else $innie1 = $_POST['innie1'];
if (!isset($_POST['outie2']))  $outie2 = ''; else $outie2 = $_POST['outie2'];

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Sessions check</title>
</head>
<body>
<h1>PHP Sessions check</h1>
<p>Remote server IP address is: <?php echo $domain ?></p>
<form action="PHPsess02.php" method="post">
<input type="text" name="innie1" value="<?php echo strtoupper ($innie1) ?>" />&nbsp;
<input type="text" name="outie2" value="<?php echo strtoupper ($outie2) ?>" />&nbsp;
<input type="submit" value="Submit" />
</form>

</body>
</html>

Open in new window