Hello, I have such a check here:

if ($_SERVER["HTTP_REFERER"] != "http://site.loc/catalog.php?id=27") { echo "текст"; } 

How to make such a check, just replace the number 27, put some template that checks if the number is true ?

    3 answers 3

    You can do this:

     <? //$ref = $_SERVER["HTTP_REFERER"]; $ref = "http://site.loc/catalog.php?id=27"; if( preg_match("/(?:\?|&)id=(\d+)(?:$|&)/", $ref) ){ echo "OK"; } ?> 

    To use remove // from line 2 and delete line 3

    UPD: maybe you need this Re:

     if ( preg_match("/^http\:\/\/site\.loc\/catalog\.php\?id=(\d+)(?:$|&)/", $ref) ){ 
    • This is the correct answer:) Oops ... the text OK is not hatching (( - dogmar
    • explain what exactly you need? <br> $_SERVER["HTTP_REFERER"] - this is where the person came from (almost the same as the previous page) <br> You may need to use $_SERVER['QUERY_STRING'] - this is the current address line data <br> - timka_s
    • In general, there are directories with such links site.loc / catalog.php? Id = 27 Only the ID is different everywhere and there is a button in each page that switches to another page, and in this page I make a rule if a person comes from directories The necessary text is displayed and so I wrote the code in the beginning, it only works on the page where ID 27 is dogmar
    • From the page site.loc / catalog.php? Id = 27 I turn to that page where I registered $ _SERVER ["HTTP_REFERER"]; and displayed site.loc / catalog.php? id = 27 - dogmar
    • Do you have such a code? $ ref = $ _SERVER ["HTTP_REFERER"]; if (preg_match ("/ (?: \? | &) id = (\ d +) (?: $ | &) /", $ ref)) {echo "OK"; } - timka_s
     $url = "http://site.loc/catalog.php?id=27"; if(preg_match("#id=(\d)#ui", $url) && $_SERVER["HTTP_REFERER"] != $url) { echo "текст"; } 

    Something like this.

    • You misunderstand, I just have a lot of pages like this: site.loc / catalog.php? id = 27 ID can be different, but now the inscription "Text" appeared only on the page where id equals 27 - dogmar
    • (\d) Replace with 27. Or read about strpos. - ling
     if(empty($_GET)) { echo 'Empty GET'; } else { if(is_numeric($_GET['id'])) { echo $_GET['id']; // Good is true // Put a template } else { echo 'Numeric is empty'; } } 

    http: //site.loc/catalog.php -> result Empty GET

    http: //site.loc/catalog.php? id = 27 -> result Good is true

    http: //site.loc/catalog.php? id = bb -> result Numeric is empty

    • Geth is not transmitted to that page)) - dogmar
    • or if (intval ($ _ GET ['id'])) ... If this is what the author means! - Alexander Maslov
    • Well, actually, if this is what the author has in mind) - Palmervan
    • there is a check via HTTP_REFERER and not $ _GET ['id'] =) - dogmar
    • and in the example, it is difficult to change $ _GET ['id'] to $ _SERVER ["HTTP_REFERER"]? the very essence of what is_numeric cleans the presence of numbers! - Palmervan