Retarded PHP question

Status
Not open for further replies.
I think this may have been touched on, but if the first character is the target, strpos would return 0, so unless you know that what you are looking for is never going to be 0th position, you would have to do something more than just check if $var was set.
 


HEh, thanks, this may be turning into PHP 101 for somlor.

Alright, so try changing my code from:
Code:
if (strpos($referer, $value) !== false ) {

to:
Code:
if (strpos($referer, $value)) {

The latter doesn't redirect to $cloaked_url, even when there is a referer match.

Sean


OK I threw up a quick test: http://design.seo-soap.com/testakl123123iosfd9/
(this won't be up long, I'll take it out later so if this post is old, don't bother :))

It tests all three methods and outputs the result both by just checking for the string (upper part), and then by checking it against a given string using strpos (below, also for all three methods).

Props to jrvan, this is very true and I completely forgot about that, lol. Using strpos this way isn't the best way because if you enter anything with "http://" into the value, it will return 0, thus be false.

This also happens when you enter "w" on the test page I linked to above. It's the first character in the string it searches for. So... either always omit writing the full url, or code up a workaround for that.

I don't know why it didn't work for you, did you maybe write http into $value?
 
Ok, now I'm obsessed.

Check it out, here is my script with your version of the if evaluation:

Code:
<?php
// simple cloaker

$referer= $_SERVER['HTTP_REFERER'];

// edit these variables
$hidefrom = array("dev.facebook", "devrs001.facebook.com", "/intern/ads/review.php");
$normal_url ="www.evil-landing-page.com";
$cloaked_url ="www.passable-landing-page.com";

// uncomment below for testing yourself as one of the referers
$referer = "dev.facebook"; 

foreach ( $hidefrom as $value ) {
    if (strpos($referer, $value )) {
        header("Location: http://" . $cloaked_url );
        exit;
    }
}

header("Location: http://" . $normal_url );
exit;

?>

Try it out. It doesn't work. It redirects to evil landing page. At least for me. (??)

Sean
 
Ok, now I'm obsessed.

Check it out, here is my script with your version of the if evaluation:

Code:
<?php
// simple cloaker

$referer= $_SERVER['HTTP_REFERER'];

// edit these variables
$hidefrom = array("dev.facebook", "devrs001.facebook.com", "/intern/ads/review.php");
$normal_url ="www.evil-landing-page.com";
$cloaked_url ="www.passable-landing-page.com";

// uncomment below for testing yourself as one of the referers
$referer = "dev.facebook"; 

foreach ( $hidefrom as $value ) {
    if (strpos($referer, $value )) {
        header("Location: http://" . $cloaked_url );
        exit;
    }
}

header("Location: http://" . $normal_url );
exit;

?>

Try it out. It doesn't work. It redirects to evil landing page. At least for me. (??)

Sean

It can't work because the position of "dev.facebook" in "dev.facebook" is 0, which will return false. That's what ryan pointed out :)

The way you did it with !== works with the temp $referer because it compares the type, and 0 is not false. Lol. I guess it depends on how you want to do it.

So in the light of this example, using strpos, I'd say you are actually right and the methods are not exactly the same. I'm used to writing it the short way and that's why I put it up like that, it was a quick answer and I should have given the strpos more thought.

Btw if you replace the temporary $referer variable with "http://dev.facebook.com", it will work because the position is not 0, but 7. This actually also closer to real life, because it is what $_SERVER['HTTP_REFERER'] returns.

So...
* my example works in real life and is shorter.
* your example works in real life and is better.
* cloaking by referer is unreliable anyway.
* omitting the type check === when using strpos this way is a bad idea.
* now let's have a drink!

Cheers :D
 
So...
* my example works in real life and is shorter.
* your example works in real life and is better.
* cloaking by referer is unreliable anyway.
* omitting the type check === when using strpos this way is a bad idea.
* now let's have a drink!
Haha... nice. First rounds on me. :)

Sean
 
Status
Not open for further replies.