Setting $_GET variables

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

qweeg
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Jun 01, 2013 5:17 am

Setting $_GET variables

Postby qweeg » Sat Jun 01, 2013 5:34 am

Hi guys,

Apologies for this newbie question - still finding my way around php.

So I have a page e.g.

Code: Select all

"index.php?p=mines&mine=1"


And on this page I have a button that links to a URL and sets a parameter for example:

Code: Select all

"index.php?p=mines&mine=1&cmd=doMining"


Then I have an if statement that checks if cmd is set and what the value of it is e.g.

Code: Select all

if (isset($_GET["cmd"]) && $_GET["cmd"] == "doMining")
{
  Do some stuff
}


This kind of works - so when the user goes to the page and presses the button they are redirected to the same page in effect but with cmd parameter set to doMining.

The problem is once the button has been pressed the

Code: Select all

if isset()
is always true, whereas I just want the code to fire once and that's it until the button is pressed again.

So what is the best way at handling this? In the If isset() should I be redirecting back to the page without parameters set or some other way of dealing with this. Or should I not be doing thiings this way at all?

Any tips would be much appreciated.

developerahusain
New php-forum User
New php-forum User
Posts: 44
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow

Re: Setting $_GET variables

Postby developerahusain » Sat Jun 01, 2013 6:19 am

This is the best way to do it
if ($_REQUEST ['cmd'] && $_REQUEST ['cmd']== "doMining")
{
echo "your stuff";
}

Any problems do revert and do come clear. I will be able to solve your problem

User avatar
Strider64
New php-forum User
New php-forum User
Posts: 77
Joined: Sat Mar 23, 2013 8:24 am

Re: Setting $_GET variables

Postby Strider64 » Sat Jun 01, 2013 7:18 am

$_REQUEST is one of the biggest design weaknesses in PHP. Every application using $_REQUEST is most probably vulnerable to Delayed Cross Site Request Forgery problems. (This basically means if e.g. a cookie named (age) exists it will always overwrite the GET/POST content and therefore unwanted requests will be performed)

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Setting $_GET variables

Postby johnj » Sat Jun 01, 2013 8:07 am

Use a form and submit button. Pass 'cmd' as a hidden variable. In this case you can always check whether the form submit button was clicked, then get the value for the hidden variable 'cmd'.

Yes, good to avoid $_REQUEST.

qweeg
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Jun 01, 2013 5:17 am

Re: Setting $_GET variables

Postby qweeg » Sat Jun 01, 2013 9:13 am

johnj wrote:Use a form and submit button. Pass 'cmd' as a hidden variable. In this case you can always check whether the form submit button was clicked, then get the value for the hidden variable 'cmd'.

Yes, good to avoid $_REQUEST.


Yep that works perfectly - many thanks!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests