Jump to content

$REQUEST_METHOD question


mrb1972

Recommended Posts

Hi,

 

Im a bit of a PHP novice so any advice would be great, I have a website written quite a while ok which is being migrated to a new server which contains a new install of php.

 

This has bought to light a few problems with the php code which my host has pointed out to me,

 

one such example is the code; which dosent get executed any more

 

if($REQUEST_METHOD=="POST" && isset($submit_x))

{

 

Im told this is no longer valid on PHP 5.1.6 , my questions is, is this easy to fix, is it a big rewite, or just a few code changes.

 

thanks

Link to comment
Share on other sites

if($REQUEST_METHOD=="POST" && isset($submit_x)) {

to

if(!empty($_POST['POST'] && isset($submit_x)) {

 

would be the immediate change...

 

..However I imagine that your code is written for 'register_globals' being on (a setting in php.ini)

which automatically makes things like session variables, form posts and url pieces into variables...

so if you go to

index.php?id=4

then $id is automatically set: $id=4;

where as now there is a seperation between such variables: they are accessed by:

$_GET['id'] // for URL things

$_POST['id'] //for POST form submission

$_SESSION['id'] //for session data.

 

Truthfully you ought to re write the code to reflect the revised best practice, but if you can persuade your host to turn register_globals back on (which they understandably may not want to) then it would probably be a quick fix.

 

Register globals info is here:

http://uk.php.net/register_globals

 

There may also be other older code in there which no longer is valid; I would recommend a good read of this page:

 

http://uk3.php.net/reserved.variables

Link to comment
Share on other sites

As was explained, your problems are all down to register_globals.

 

Just thought i would add that the $REQUEST_METHOD variable is part of the $_SERVER superglobal array. So, with register_globals on, you would access its contents with $_SERVER['REQUEST_METHOD'];

Link to comment
Share on other sites

As was explained, your problems are all down to register_globals.

 

Just thought i would add that the $REQUEST_METHOD variable is part of the $_SERVER superglobal array. So, with register_globals on, you would access its contents with $_SERVER['REQUEST_METHOD'];

 

Thanks for your comments so far, I have indeed tried the $SEVER['REQUEST_METHOD']; but it didnt seem to work, everything after the IF statment is ignored when the submit button is pressed

 

<?

include("includes/config.php");

 

$message = "";

 

if($REQUEST_METHOD=="POST" && isset($submit_x))

{

 

And the form code is , does the $PHP_SELF self part need changing?

 

<form name="frm" method="post" action="<?=$PHP_SELF?>">

 

thanks

 

 

Link to comment
Share on other sites

I would change your if statement to:

 

if(isset($_POST['submit_x'))
{
}

 

Again, due to register_globals being off, the variable submit_x isn't defined. It is contained within the $_POST superglobal array. You'll need to change all of your variables that are from your form to be taken from the $_POST array.

 

The reason why i dropped the request method part of your if statement is that if something is set inside the $_POST array, then the method is going to be post.

Link to comment
Share on other sites

No its not a lot of code here is the php part, the bit i havent posted is just the html form which is below the php.. the problem is it just seems to ignore everything in (If statment) ..thanks

 

<?
include("includes/config.php");

$message = "";

if($REQUEST_METHOD=="POST" && isset($submit_x))
{
if(empty($vchUserName))
	$message .= "Please enter your username.<br>";
if(empty($vchPassword))
	$message .= "Please enter your password.<br>";
if(empty($vchRePassword))
	$message .= "Please confirm your password.<br>";
if(!empty($vchPassword) && !empty($vchRePassword))
{
	if(trim($vchPassword) != trim($vchRePassword))
	{
		$message .= "Passwords don't match.<br>";
	}
}
if(empty($vchEmail))
	$message .= "Please enter your e-mail address.";


if(empty($message))
{

	//check for the existence of the username and the password
	$exist = GiveValue("count(intMemberID) as cnt","tblMembers"," where vchUserName = '".$vchUserName."'",0);

	$exist1 = GiveValue("count(intMemberID) as cnt","tblMembers"," where vchEmail = '".$vchEmail."'",0);

	//print $exist;
	if($exist>0 || $exist1>0)
	{
		$message = "Duplicate username or e-mail address. Please try again";
	}
	else
	{
		//insert
		$sql = "insert into tblMembers(vchUserName,vchPassword,vchEmail,chStatus,dtAdded) values('$vchUserName','$vchPassword','$vchEmail','D',now())";
		//print $sql;
		mysql_query($sql);
		$MID = mysql_insert_id();

		if(mysql_insert_id())
		{
			//send email
			$myname = "New Zealand Stays";
			$myemail = "listings@newzealandstays.co.nz";
			$contactname = $vchUserName;
			$contactemail = $vchEmail;
			$strmessage = "Dear $contactname,<br/><br/>Thank you for registering on Newzealandstays, please confirm your registration by clicking on the following link to activate your membership account - <br><br><a href='http://$HTTP_HOST/confirm.php?user=$MID'>http://$HTTP_HOST/confirm.php?user=$MID</a><br><br>After confirmation you can use the members area to log-in and add your property on the website.<br><br>Regards,<br>Newzealandstays Administrator<br><br><a href='http://$HTTP_HOST'>$HTTP_HOST</a>";
			$subject = "Confirmation email from - ".$HTTP_HOST;
			$headers .= "MIME-Version: 1.0\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1\n";
			$headers .= "From: ".$myname." <$myemail>\n";
			$headers .= 'Cc: listings@newzealandstays.co.nz' . "\n";
			mail($contactemail, $subject, $strmessage, $headers);
			$message = "A confirmation email has been sent to the email address you provided, please check your email and confirm your registration. Thank you";
			//print $strmessage;

		}
	}
}

}
fnHeader();
?>

Link to comment
Share on other sites

Let me explain what "register globals" does....

In the standard set up of PHP5, all posted form variables, url strings and session info is put into arrays called $_POST, $_GET and $_SESSION respectively...

So in your script there are variables called $_POST['vchEmail'] - which the script expects to be called $vchEmail...

so you need to identify every variable whichcame from a script, and rename it....

 

I would image your final script to be:

 

<?
include("includes/config.php");

$message = "";

if(isset($_POST['submit_x'))
{
if(empty($_REQUEST['vchUserName']))
	$message .= "Please enter your username.<br>";
if(empty($_REQUEST['vchPassword']))
	$message .= "Please enter your password.<br>";
if(empty($_REQUEST['vchRePassword']))
	$message .= "Please confirm your password.<br>";
if(!empty($_REQUEST['vchPassword']) && !empty($_REQUEST['vchRePassword']))
{
	if(trim($_REQUEST['vchPassword']) != trim($_REQUEST['vchRePassword']))
	{
		$message .= "Passwords don't match.<br>";
	}
}
if(empty($_REQUEST['vchEmail']))
	$message .= "Please enter your e-mail address.";


if(empty($message))
{

	//check for the existence of the username and the password
	$exist = GiveValue("count(intMemberID) as cnt","tblMembers"," where vchUserName = '".$_REQUEST['vchUserName']."'",0);

	$exist1 = GiveValue("count(intMemberID) as cnt","tblMembers"," where vchEmail = '".$_REQUEST['vchEmail']."'",0);

	//print $exist;
	if($exist>0 || $exist1>0)
	{
		$message = "Duplicate username or e-mail address. Please try again";
	}
	else
	{
		//insert
		$sql = "insert into tblMembers(vchUserName,vchPassword,vchEmail,chStatus,dtAdded) values('$_REQUEST['vchUserName']','$_REQUEST['vchPassword']','$_REQUEST['vchEmail']','D',now())";
		//print $sql;
		mysql_query($sql);
		$MID = mysql_insert_id();

		if(mysql_insert_id())
		{
			//send email
			$myname = "New Zealand Stays";
			$myemail = "listings@newzealandstays.co.nz";
			$contactname = $_REQUEST['vchUserName'];
			$contactemail = $_REQUEST['vchEmail'];
			$strmessage = "Dear $contactname,<br/><br/>Thank you for registering on Newzealandstays, please confirm your registration by clicking on the following link to activate your membership account - <br><br><a href='http://$HTTP_HOST/confirm.php?user=$MID'>http://$HTTP_HOST/confirm.php?user=$MID</a><br><br>After confirmation you can use the members area to log-in and add your property on the website.<br><br>Regards,<br>Newzealandstays Administrator<br><br><a href='http://$HTTP_HOST'>$HTTP_HOST</a>";
			$subject = "Confirmation email from - ".$HTTP_HOST;
			$headers .= "MIME-Version: 1.0\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1\n";
			$headers .= "From: ".$myname." <$myemail>\n";
			$headers .= 'Cc: listings@newzealandstays.co.nz' . "\n";
			mail($contactemail, $subject, $strmessage, $headers);
			$message = "A confirmation email has been sent to the email address you provided, please check your email and confirm your registration. Thank you";
			//print $strmessage;

		}
	}
}

}
fnHeader();
?>

 

but you do need to check this.

 

I have used $_REQUEST, a combination of $_POST and $_GET, which should work for you.

Link to comment
Share on other sites

Well are you doing a mod_rewrite or is the file being called included through a different file?

 

Remember PHP thinks PHP_SELF is the original script. So if you have a mod_rewrite to send let's say www.yourdomin.com/help to www.yourdomain.com/main.php?action=help  well the main.php is going to be the php_self file, not help/index.php

 

Just a little fyi.

Link to comment
Share on other sites

Yes thanks i have tried that, no joy..

 

I even got the hosting company to turn register back on, but still the form will only work with php_self part removed..

 

??? I give up..

Rather than turn regsiter_globals on you should set error_reporting to E_ALL  and turn display_errors on. That way PHP will spit out errors if there is any when the script runs.

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.