Using PHP $_SERVER[HTTP_REFERER] to display custom welcome messages/content - php

I'm trying to display a custom welcome message to visitors based off what social media site they are coming in from. I can't get this string to work but it is echoing back no matter what the refering site is. Can anyone help me get this to work? Many thanks!
<?php
if (strpos("twitter.com",$_SERVER[HTTP_REFERER])==0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
?>
Tried using
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
echo $_SERVER['HTTP_REFERER'];
}
?>
to get this to work but no luck.

strpos returns false when the search word does not appear in the string. 0 also evaluates to false when compared to a boolean. So false == 0, and your code always runs.
Use a strict comparison to require both value and type match instead of ==
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) === 0) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}
However, the referrer will not start with twitter.com, it'll start with http:// or https:// so your condition wasn't right in the first place. To search for twitter.com anywhere else in the string:
if (strpos("twitter.com", $_SERVER['HTTP_REFERER']) !== false) {
echo "Welcome, Twitter User! If you enjoy this post, don't hesitate to retweet it to your followers";
}

I think you may have confused strpos() with strcmp(), being strcmp() returns 0 when two strings are equal. strpos() returns the position at which a string was found. Try:
<?php
if (strpos('twitter.com', $_SERVER['HTTP_REFERER'] != 0)) {
echo "Welcome, twitter user.";
}
?>

Related

Using || in if statements

If a user visits test.php?number=1 or test.php?letter=1, I want them to stay on the page. If they visit test.php, I want them to be re-directed.
I have tried to accomplish the above using the script below, however the following happens:
if the user visits test.php?number=1, they stay on the page.
If the user visits test.php?letter=1, they are re-directed.
if(!isset($_GET['number']) === false || $_GET['letter'] === false) {
echo '<h1>Hello</h1>';
}else{
header('Location: redirect.php');
exit();
}
Note: the value of number and letter can change! It won't always be 1!
Instead of isset() use empty() in this case as an non-existent value does not do you any good, ie. test.php?number= with no value entered for number. Ditto for letter.
The following will check if either number or letter contain a value, to which the condition will satisfy and <h1>Hello</h1> will print. Otherwise, if neither contain a value, you will be redirected.
if ( ! empty($_GET['number']) || ! empty($_GET['letter']) ) {
echo '<h1>Hello</h1>';
}
else {
header('Location: redirect.php');
exit;
}

Show HTML if URL contains a defined domain name, else show something else

I am building an admin panel that can be used on different sites. I need to show some specific HTML if the url contains a specific domain, and something else if not.
So, if the URL is something like this http:// (something) .mydomain.com/admin.... I would like to show some HTML.
If the URL is something like this http:// anotherdomain.com/admin... I would like to show something else.
Í have tried with this, but somehow didn't get that to work. What am I doing wrong?
Edit: Wrong code. See code below...
<?php
if (substr($_SERVER['REQUEST_URI'], 0, 5) !== '/mydomain.com') {
echo '<div id="stuff"></div>';
}
?>
Sorry: Posted the wrong php code. A bit too tired I think...
This is the one I messed with:
<?php
if (substr($_SERVER['SERVER_NAME']) !== 'mydomain.com') {
echo '<div id="stuff">test</div>';
}
?>
Please try below code
<?php
$url_segments = parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
if (strpos($url_segments['host'],'mydomain.com') !== false) {
echo 'true';
}else{
echo 'false';
}
?>
Thanks,
Try with that code:
if (preg_match ( string $pattern , string $subject)){}
http://php.net/manual/de/function.preg-match.php
http://php.net/substr
Your current substr is saying "If the first 5 characters of the request URI aren't /mydomain.com, print the stuff below."
You could try to do this correctly in a few ways. You could use strpos and checking to see if mydomain.com appears in the request URI, or a more complicated regex to find something like (mydomain.)
Change $_SERVER['REQUEST_URI'] for $_SERVER['SERVER_NAME'] if mydomain.com it's your domain name.
Also, the substr is returning 5 chars and you are comparing against 13 chars

simple if else or preg_match expression

I have an example.php form when posted to perl script example.pl which responds either this
<!-- comments disabled -->
or this
<!-- comments enabled -->
The above response is stored in php get request in $commentstatus variable
Response may be a multiline or single line !
How to build an expression or verify the response using php preg_match or a normal validation for if else condition in php?
You can check for the presence of a string, within a string with strpos().
So the actual code would be:
<?php
if (strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
elseif(strpos($commentstatus, "<!-- comments disabled -->") !== false)
{
}
function commentsEnabled($str){
preg_match_all('/^.*?<!--\s*comments\s+(en|dis)abled\s*-->.*$/m', $str, $m);
return !count($m[1]) ? null : array_pop($m[1]) == 'en';
}
demo
enlarge image
Credits to #lbu for the SIMPLEST solution who commented under the Op
This works excellently and is the easiest
$content= trim($commentstatus);
if ($content == "<!--commentsdisabled-->"){
print "Dear User Comments are disabled for your access";
}else if($content == "<!--commentsenabled-->"){
print "Dear User Comment added to blog post entry";
}else{
print "You are Banned for Spamming";
}
#degenerate
"Learn how to read the PHP documentation," - rude and assumptious
But your code works, even for line breaks read in from a file.
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
I don't know how it works, because the documentation you linked says to use the 's' switch :
"The s tells the code NOT TO stop searching when it encounters \n (line break)"
This works is with preg_match_all & preg_match !!
It's a plus 1 even with the ending snark!
Not sure what #BetaCoder is on about, but it's obviously not a question.
Not sure if you are simply asking this?
if (preg_match('#enabled#', $commentstatus)){
// Enabled code
}else{
// Everything else code
}
Learn how to read the PHP documentation, please:
http://us1.php.net/preg_match

How to search in the source code for a string with php?

I have tried
<?php
$url = $_POST['attributename'];
$needtofind = "did not match any documents. </p>";
$site = file_get_contents("https://www.google.com/#q=site:$url");
if(strpos($site, $needtofind) == false) {
echo 'indexed';
} else {
echo 'not indexed';
}
ob_end_clean();
?>
HTML
<div class="center-page">
<form method="POST">
<textarea id="float" name="attributename" value=""></textarea><br/>
<input type="submit" value="Go" />
</form>
</div>
Codes are on the same page. I just typed them like this to be more clear.
Main problem is that by default it tells me on the screen indexed. If i type any url it will say as well indexed. For example I type the url in the textarea jhbsadhbahsd545.com, it returns indexed when it should have returned not indexed.
What have I done wrong?
strpos can return 0 which is a falsy value. Compare with ===
strpos($site, $needtofind) === false
However I believe this won't work as Google does not return the string with the first response that you are looking for, but rather lazy loading once the page has been loaded with javascript.
Open up Chrome and view-source:https://www.google.com/#q=site:hopefullythisisadomainthatdoesnotexists.com to check what does Google return and why is it always missing.
Also change the URL you are making the request to from:
https://www.google.com/#q=site:$url
to:
https://www.google.com/search?q=site:$url
So you cannot scrape content from Google that way, they actually prohibit you from doing it. You'll need to utilize their API to do what you're needing.
https://developers.google.com/custom-search/json-api/v1/overview

$_SERVER['HTTP_REFERER'] does not work alltime

I am filtering a page with $_SERVER['HTTP_REFERER'].
Let "pageb.php" filter whether a user have come from "pagea.php" (although it is not reliable) by $_SERVER['HTTP_REFERER'].
It works well until "pagea.php" has a preceding query string like "pagea.php?q=10". Then if a user goes from "pagea.php?q=10" to "pageb.php" it is not detected by "pageb.php":
if($_SERVER['HTTP_REFERER']='http://pagea.php'){
echo 'This user has come from page a';
}else{
echo 'This user has come from another page';
}
How can I detect a user came from "pagea.php" even with preceding query string q=10?
Use strpos() to check if a string contains another string:
<?php
if (strpos($_SERVER['HTTP_REFERER'], 'pagea.php') !== false) {
echo 'from page a';
}
use comparision operator not assignment
if($_SERVER['HTTP_REFERER']='http://pagea.php'){
= will be ==

Categories