Page 1 of 1
if the page location contains a certain string...
Posted: Tue Mar 30, 2004 4:24 am
by batfastad
Hi,
I'm new to PHP.
I'd like to get a script to detect if a certain piece of text is in the address bar of the current page.
I'm looking for something similar to window.location in javascript.
Basically I want users to go to
http://www.site.com/options.php but there are two links to the options.php page...
One with the href
http://www.site.com/options.php&Token.0=1 and the other with the href
http://www.site.com/options.php&Token.0=2
Then on the options.php page I want to display certain text if token.0=1 and different text if the value of token.0 is 2
Basically I need to know how to use an if statement to do different stuff depending on what text is in the address bar.
Any help will be appreciated. I've been looking through the manual a bit but can't find any help.
Thanks
Batfastad
Posted: Tue Mar 30, 2004 4:55 am
by CoderGoblin
given that
http://www.site.com/options.php?token=1 and
http://www.site.com/options.php?token=2...
My personal opinion, but I don't like the .0 in the name if not required and I would make it all lowercase.
Nothing to do with the actual address. You would need to look at the superglobals $_GET values if these values are not send via POST with a form (In that case you check $_POST).
Code: Select all
if ($_GET['token']==1) {
// do something
} else {
// do something else
}
I would advise you to look up the following in the PHP Manuals
IF
SWITCH
$_GET
$_POST
and for the server/php settings register_globals. This is OFF by default and should remain so.
you could also use the command phpinfo(); to get a list of other global variables.
Posted: Tue Mar 30, 2004 5:19 am
by twigletmac
Also you need to check that the variable is set in the URL before trying to use it - remember your users can see it and modify it.
First get rid of any leading or trailing spaces:
Code: Select all
foreach ($_GET as $key => $value) {
$_GET[$key] = trim($value);
}
then test to see whether the variable is set and set it to a default if it's not (you can also test to make sure the value is a number):
Code: Select all
$token = (!empty($_GET['token']) && is_numeric($_GET['token'])) ? $_GET['token'] : 1;
now when you do the if...elseif...else statement or the switch statement you can test the $token variable's value.
Mac
Posted: Tue Mar 30, 2004 6:41 am
by batfastad
just spent the last hour or so messing about...
I've got a question about syntax...
twigletmac...
on the line
$token = (!empty($_GET['token']) && is_numeric($_GET['token'])) ? $_GET['token'] : 1;
what does the && represent?
what does the ? represent?
is that so you can do... if is not empty and is numeric???
Is there something wrong with me doing this...
<?php
if ($_GET['type']==error) && ($_GET['mag']==idn) {
print "type=error and mag=idn";
} else {
print "type and mag are something else";
};
?>
as I can't get it to work.
Thanks
Batfastad
Posted: Tue Mar 30, 2004 7:12 am
by twigletmac
Regarding:
Code: Select all
$token = (!empty($_GET['token']) && is_numeric($_GET['token'])) ? $_GET['token'] : 1;
you are correct, the && means and (|| would mean or), so the code checks that the variable is not empty AND that it is a number. The question mark is part of a thing called the ternary operator, it's a simplified (IMHO) if...else statement.
Instead of:
Code: Select all
<?php
if ($_GET['type']==error) && ($_GET['mag']==idn) {
print "type=error and mag=idn";
} else {
print "type and mag are something else";
};
?>
try
Code: Select all
<?php
// * put the parenthesis around the entire if condition
// * put the strings to be compared in quotes (unless you are comparing
// the variables to constants
if ($_GET['type'] == 'error' && $_GET['mag'] == 'idn') {
print "type=error and mag=idn";
} else {
print "type and mag are something else";
}
// you don't need a semi-colon after the closing brace so you can have
// } instead of };
?>
Mac
Posted: Tue Mar 30, 2004 7:20 am
by batfastad
Excellent - thanks!!
I'm getting there slowly with PHP.
Thanks for your help again guys - I've got enough to be getting on with now.
Batfastad
Posted: Tue Mar 30, 2004 7:25 am
by batfastad
so the question mark is a way of writing if... and... otherwise...
Thanks again
Posted: Tue Mar 30, 2004 7:27 am
by twigletmac
Yup, basically it's (in pseudo code):
Code: Select all
$var = (condition to test) ? /*if condition is true*/ : /*if condition is false*/;
which equates to:
Code: Select all
if (condition to test) {
/*if condition is true*/
} else {
/*if condition is false*/
}
Mac