Show Different "Code/Words" Based on URL Referer - php

I'm trying to have my website not show certain code if they come from certain URL's.
For example, Wikipedia don't like links to sites that have popups on them. So I need to not show the code for that referer.
I found the following code but it doesn't seem to work when code is places instead of text
<?php $ref=getenv('HTTP_REFERER');
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>

If you want to avoid showing code to google:
<?php if (!strstr(strtolower($_SERVER['HTTP_USER_AGENT']),"googlebot")){ ?>
//Show what you want, google will not see it
}else{
//show other code
}?>?>
For wikipedia:
<?php if (!strstr(strtolower($_SERVER['HTTP_REFERER']),"wikipedia")){ ?>
//Show what you want, wikipedia will not see it
}else{
//show other code
}?>
Enjoy ;)

You were talking about Wikipedia. Probably the problem is that google isn't sending you "google" in their referer string

it must work, try alternative variable
<?php
$ref=$_SERVER['HTTP_REFERER'];
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>

Related

How do I make this php URL transition work?

Basically my task is,through the mandatory use of _GET,I'm supposed to make a code that fetches a specific php file when someone types in something like ..php?page=airlines and I dunno if I'm just lacking information or what but this isn't working router.php:
<?php
$nav =array("home"=>"C:\xampp\htdocs\project\home.php",
"flight"=>"C:xampp\htdocs\project\flight-detail.php",
"order"=>"C:xampp\htdocs\project\order-flight.php,",
"testimonial"=>"C:xampp\htdocs\project\add-testimonial.php");
if ( isset($nav[$_GET['page']]) )
{
echo header('Location: ' . $nav[$_GET['page']]);
}
if i understood your question, there are many ways to reach your goal.
I write an example that no change very your code:
<?php
$myPath="C:\xampp\htdocs\project\";
$nav =array("home"=>"home.php",
"flight"=>"flight-detail.php",
"order"=>"order-flight.php,",
"testimonial"=>"add-testimonial.php");
if ( isset($_GET['page']) and !empty($_GET['page']))
{
$myFile=nav[$_GET['page']];//i assume that in page there are only array keys value
echo header('Location: ' . myPath.$myFile);//i.e. I suppose in page there is home output is: "C:\xampp\htdocs\project\home.php"; file
}
else
{
echo 'error: page not found!';
}
An other "easy" solution is to create in first page the link where the user can choose the page:
<a href="C:\xampp\htdocs\project\home.php">HOME<a>
<a href="C:\xampp\htdocs\project\flight-detail.php">FLIGHT DETAIL<a>
....
Hope this helps

How to close brakets in php $_SESSION?

when i use first code it works but not working for second one.
first code that works fine
<?php
session_start();
if(!isset($_SESSION['user_email'])){
//if not logged in
header ("location: login.php");
}
else {
?>
welcome page
<?php } >
But when i try to do this only "if" statement is working "else" is showing nothing
<?PHP
session_start();
if(!isset($_SESSION['user_email'])){
?>
html codes here (works fine )
<?php}
else { ?>
diffrent html codes here
(but this section is not working)
<?php }?>
what is wrong and how should i close "{} " in this case ...please help
Hi Jannatul!
In your second PHP opening tag, you have this:
<?php}
This without a space may be causing issues while parsing your code. Try and replacing it to this:
<?php }
If this works please tell me so, and if it doesn't I will gladly be willing to assist you further.
Matt
Try this without that mess..
<?php
session_start();
if(!isset($_SESSION['user_email']))
{
echo 'html codes here (works fine )';
}
else
{
echo 'diffrent html codes here (but this section is not working) - but maybe now does';
}
?>

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

disable a href links using PHP

I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">

how to make redirect that includes data about the link that was clicked?

I have a page with two links (text link & banner link),
that should lead to the same redirect page
(on my domain).
The redirect would be to a link that shall include a variable,
that indicates which of the two link was clicked.
e.g.:
<?php
header("Location: http://external-domain.com/?ref=[value]");
die();
?>
wheareas the "value" should be "text" / "banner" or something similar.
How can I do this?
I'm not a web programmer at all so I don't have much technical knowledge,
I guess one possible solution (which I would rather avoid) would be to give a separate id for the text link and for the banner, e.g.:
text link:
http://mydomain.com/redirect.php?id=text
banner link:
http://mydomain.com/redirect.php?id=banner
whereas redirect.php would contain:
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
But that would mean that I have to use a different id for these internal links;
I would rather find a way to use the same exact internal links (maybe for example, have each link in a "div" or a "class" and get the div/class name somehow, and have them appear as the [value].
*EDIT:
Let me emphasize again: I'm looking for a way to do this without having to use any "?id=[something]" at the end of the text link or the banner link.
Thanks!
If you're already outputting the banner/text do this for the banner
<img src="imageHere.png">
And this for the text
text here
Then catch the id get values with
<?php
$id = $_GET['id'];
if ($id == 'banner'){
echo 'The clicked link was a banner';
}
else{
echo 'The clicked link was text';
}
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
should be
<?php
$source = $_GET['id'];
header("Location: http://external-domain.com/?ref=".$source);
die();
?>
Basic concatenation.

Categories