Jump to content

query string and isset

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
jonnyfolk

jonnyfolk

    Newbie

  • Members
  • Pip
  • 9 posts
Hi,

I normally program in perl and am trying to translate one of my scripts to php.

The following snippet doesn't set the variable and I thought it would!
What the script does is to redirect to a different script depending on whether a query exists, and depending on the existence of a parameter if there is a query string.

I would like to know why, when calling without a query $scriptrl is not being set, and also how do I query a parameter from a GET call (equivalent of CGI
$var = param('variable');

Thanks,
Jon

$query = $_SERVER["QUERY_STRING"];

if(! isset($query))
{
$scriptrl = 'g_search.php';
}

Edited by jonnyfolk, 03 March 2010 - 02:17 PM.
changed script name for privacy reasons - no impact on thread!


#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
EDIT: nvm.

Try:

if(!isset($_SERVER['QUERY_STRING']))

or to check if it's empty:

if(empty($_SERVER['QUERY_STRING'])) 

Not sure if this is what you mean?

As your script's if loop will always return true as before the loop you set the variable $query ( and the function isset is independent of whether a variable is empty or not, just cecks if it's set - it's set ).

#3
jonnyfolk

jonnyfolk

    Newbie

  • Members
  • Pip
  • 9 posts
I see - I hadn't realised that consequence. I chose the 'empty' option and it worked perfectly. Thanks.

Regarding the param from the GET call, eg script.php?var=one&var1=two&var2=three how to I get the var1 value?

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
isset and empty is a little special and can not always be trusted, as some can be true even if they have the value 0 (zero). The safest way is actually to check with if ($var == "").

you get the var1 with $_GET['var1']
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Glad to hear that it's working now :)

@Orjan True, thanks for heading that up, I experienced that problem with the value '0' a while ago indeed, forgot about that but I think that was with isset function.
Not sure if this would be a problem in this case though.

And the GET var1 can indeed be accesed within the GET array with as sub-variable name 'var1', as in general it's:

$_GET['sub_variable_name']

So when adding a variable through url called 'var1' it automaticly is converted into a GET variable $_GET['var1'].

Cheers.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
isset vs. empty John Ciacia

#7
jonnyfolk

jonnyfolk

    Newbie

  • Members
  • Pip
  • 9 posts
Thank you for your input. I appreciate the responses and will consider the information I've been given. A very interesting (and successful) first day on the boards!!

Jon