FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


to compare URL (address line) with some my variable





oleszka
what php code i should write to compare URL (in browser-address line) with some my variable.
for example in browser i typed such url http://www.mysite.com/cat.php?=r3
so in cat.php file must be some code what can compare this "r3" with my variable $nop (for example $nop=r4)
how can I do so? to compare URL (in browser-address line) with some my variable

sorry if i made mess. i am not good at php :-S
MrBlueSky
You can do this:

Code:


<?php

  if (preg_match('/^=([^&]*)/', $_SERVER['QUERY_STRING']), $res) {
    if ($res[1]=="r3") {
      // Url is http://www.mysite.com/cat.php?=r3
    }
    if ($res[1]=="r14") {
      // Url is http://www.mysite.com/cat.php?=r14
    }
    if ($res[1]==$nop) {
      // Url is http://www.mysite.com/cat.php?=$nop
      // etc.
    }
  }
 
?>


Put your own code instead of the comments
sonam
You can use globals, too.

Code:
<?php
$check = "r14";
if(strpos($_SERVER['REQUEST_URI'], $check) === FALSE) {
     echo "NOT FIND";
} else {
     echo "HERE IS";
}
?>


Sonam
MrBlueSky
sonam wrote:
You can use globals, too.

Code:
<?php
$check = "r14";
if(strpos($_SERVER['REQUEST_URI'], $check) === FALSE) {
     echo "NOT FIND";
} else {
     echo "HERE IS";
}
?>


Sonam


Be carefull if you do it this way, because it will look for the string in the entire URL. You don't want to set $check to "w" for example, or any other string already contained in the URL. Especially if you also use other GET fields:

http://www.mysite.com/cat.php?=r3&name=roger14
mariohs
Well, if you want just to compare the r3 thing, why don't you name it, for example:
http://www.mysite.com/cat.php?q=r3
and then, just do this:
Code:
$nop = $_GET['q'];


Then you can have as many variables as you want...
http://www.mysite.com/cat.php?a=1&b=2&c=3

So you can get the 1, 2 and 3 values by doing
Code:
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
sonam
@MrBlueSky
Quote:
Be carefull if you do it this way, because it will look for the string in the entire URL. You don't want to set $check to "w" for example, or any other string already contained in the URL. Especially if you also use other GET fields:


I am sugest this option based on Oleszka post. Oleszka didn't ask "How I will get some information from url." Question is:
Quote:
so in cat.php file must be some code what can compare this "r3" with my variable $nop (for example $nop=r4)
how can I do so? to compare URL (in browser-address line) with some my variable


and I am not going over this question. If he/she use GET option then he/she cannot compare string with URL, isn't it? Also, $_SERVER['REQUEST_URI'] give only folder (if is file in subfolder) and file name without server name. In that case is quite simple to find some string what haven't same name like folder or file and compare it wiht exist variable.

I don't know why Oleszka need this comparation, but my answer is apsolutely corect. Very Happy

Sonam
oleszka
Code:
<?php

  if (preg_match('/^=([^&]*)/', $_SERVER['QUERY_STRING']), $res) {
    if ($res[1]=="r3") {
      // Url is http://www.mysite.com/cat.php?=r3
    }
    if ($res[1]=="r14") {
      // Url is http://www.mysite.com/cat.php?=r14
    }
    if ($res[1]==$nop) {
      // Url is http://www.mysite.com/cat.php?=$nop
      // etc.
    }
  }
 
?>

this gives me error :
Quote:
Parse error: parse error in c:\appserv\www\sites\cat.php on line 3


but this works
Code:
<?php
$check = "r14";
if(strpos($_SERVER['REQUEST_URI'], $check) === FALSE) {
     echo "NOT FIND";
} else {
     echo "HERE IS";
}
?>



why I can not rename it like this q=r3 because in script of catalog what i found makes url like this cat.php?=r14 ... and what I want to do by comparing url with my variable... if for example r14 (it shows one category of sites-for example Food) so by comparing i can upload on page articles about Food, if it is r3 = "Car Category of sites" so i can upload articles about car on that category... Rolling Eyes may be it's weird and can have easer solution... but for me it's better to understand Smile
MrBlueSky
Sorry, typo. It should be this:

Code:

<?php

  if (preg_match('/^=([^&]*)/', $_SERVER['QUERY_STRING'], $res)) {
    if ($res[1]=="r3") {
      // Url is http://www.mysite.com/cat.php?=r3
    }
    if ($res[1]=="r14") {
      // Url is http://www.mysite.com/cat.php?=r14
    }
    if ($res[1]==$nop) {
      // Url is http://www.mysite.com/cat.php?=$nop
      // etc.
    }
  }
 
?>
Related topics
Problem with Subdomains
loadurl
Creating a new Operating System
foobar or winamp?
Perl server referencing
Check strange file(or your computer) for viruses on-line
Fields appearing in URL address bar
Can I refresh a page using php?
Receba Revista Veja e Caras de Graηa
URL in Title Tag
adding logo to url
METAR data
Where to Learn the Correct Syntax for php
Improve Frihost with (gr)8 Greasemonkey scripts
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.