Wordpress PHP if iframe external domain then - php

I try to make something like title say's.
Some friendly websites use a widget with iframe of my front page and i get traffic.
The problem is that all the traffic goes to home page. I want to share the visitors of the iframe to random posts without to edit the external iframe widget of the friend's site's.
i have search and finded a code but has a bug and with this code i cant access my homepage, the home page is redirecting all visitors to random posts.
How can i make the home page redirecting if it is only in iframe?
<?php
$continue = 0;
if(isset($_SERVER['HTTP_REFERER'])) {
//correct domain:
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
} else {
$continue = 1;
}
}
if($continue == 0){ ?>
<?php if (is_front_page()) { ?>
<?php query_posts( 'posts_per_page=1&orderby=rand' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wp_redirect ( get_permalink () ); exit; ?>
<?php endwhile; ?>
<?php } ?>
<?php } ?>
Thanks

Your code is confusing. Why do you need to store "continue" as a variable? Why don't you just add something like this to the top of your page?
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
header('Location: http://www.yourwebsite.com/posts_per_page=1&orderby=rand');
exit();
}
}
?>
(Just reusing your code - basically, check if the site is loaded in an iframe and the homepage was requested - if so, redirect to the random page)

Related

parameters in url is automatically deleted in wordpress

I built a custom plugin on which I get the value of parameter to validate my page,
Here is my sample code:
www.test.com/user/?page="Parameter"
<?php if(empty($_REQUEST['page'])) { ?>
Landing Page
<?php }else if( $_REQUEST['page'] == 'add_User'){ ?>
Add User
<?php }else if( $_REQUEST['page'] == 'edit_User'){
Edit User
<?php } ?>
But my problem is wordpress automatically removes the page="Parameter part".
"?page" is a reserved word that is why it is automatically culled.

Page redirection issue in wordpress

I want to redirect to another page or website in WordPress. I am using below code In which $page variable contains the URL of the website and want to redirect it with a tag href. Problem is the link is appended to localhost current page URL.
the code is here
$page= get_post_custom_values('projPage');
if($page[0] != ""){
var_dump($page);?>
<p>
Project Page
</p> <?php
} else { ?>
<p><em>Project Page Unavailable</em></p><?php
}
Your URL should have a protocol like HTTP or HTTPS else it is considered as a relative path.
$page[0]= 'http://www.google.com';
if($page[0] != ""){
var_dump($page);?>
<p>
Project Page
</p> <?php
} else { ?>
<p><em>Project Page Unavailable</em></p><?php
}
this one is works fine for me

magento code for getIsHomePage - with a twist

Not sure how to explain this but i'm trying to run (2) php if statements together using a getIsHomePage block of code. I need for the div replacement to be true if home page = yes and if "certain page url" = yes. If not either of these two pages then the 'else' statement should be true.
Here is the code block I have now:
<?php if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()) {
echo '<div class="main">';
} else {
echo '<div class="main container">';
}
?>
Any assistance would be appreciated.
Thanks,
Ryan.
You can use the below-mentioned code to check if you are on Home page or viewing your custom URL.
<?php
$myCustomUrl = "...";
if($this->getIsHomePage() || $myCustomUrl ) {
echo 'Either you are on Homepage or your page URL is : ' .$myCustomUrl;
} else {
echo 'Nither you are on Homepage nor your page URL is : ' .$myCustomUrl;
}
?>

Display html content only in home page

I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,
<?php
if host == 'http://domain.com/'; echo {
This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
} else {
};
?>
<?php
$match_domains = array('google.com','www.google.com');
if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
echo 'show on my domain!';
}
?>
Using $_SERVER['SERVER_NAME'] we compare it to our desired domain.
We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.
<?php
$domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
$match_domains = array('google.com');
if(in_array($domain, $match_domains) {
echo 'show on my domain!';
}
?>
Since you want the home page why don't you check the file name
if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
echo "<div>Content</div>";
}

$_SESSION Help How To Use It In One Page? (With Redirect?)

I wonder if the following is possible?
My website have a secret link (website.com/?secret=yes) I wanted to make the url look (website.com) after they have entered + show them the special content because they are from the secret link.
I thought about something like this possibly can work?
1. User Navigate to (website.com/?secret=yes) create a $_SESSION and make it true + Instant navigate to website.com
2. Checks to see if $_SESSION = true if true show the special content?
I have the the following code:
<?php $secret = isset( $_GET[ 'secret' ] ) ? sanitize_text_field( $_GET[ 'secret' ] ) : false; ?>
<?php if( 'yes' === $secret ) : ?>
<div>
<p>My secret content</p>
</div>
<?php endif; ?>
If it was possible to make use of this + $_SESSION or if you have any ideas? I don't really know how $_SESSION works but i read in php.net about it and i think it's possible?
Thank you!
p.s I use wordpress.
You could try this inside of your index.php page:
<?php
session_start();
if(isset($_GET['secret']) && $_GET['secret'] === 'yes') {
$_SESSION['secret'] = true;
header('Location: www.website.com');
}
if(isset($_SESSION['secret']) && $_SESSION['secret'] === true) {
//Yay! Display secret content
}
?>
I've rewritten your code to work without a redirect at all. The secret sections can exist in the same page as the landing page, or in different pages. I've also modified the logic so that it will not "forget" the secret status if they come back to the landing page without "?secret=yes" in the URL. I've also updated the code with some basic Javascript that will allow you to remove the "?secret=yes" from the URL without redirecting.
This code would go in any landing page:
<?php
session_start();
if(isset($_GET['secret']) && 'yes' === $_GET[ 'secret' ])
{
$_SESSION['secret'] = true;
}
?>
This code would go in the head section of your page, or the body section if you can't access the head.
<?php
if(isset($_GET['secret']) && 'yes' === $_GET[ 'secret' ])
{
echo '<script>history.pushState({},"","http://yourdomain.com/pageinurl/");</script>';
}
?>
This code would go on any page with secret code:
<?php if(isset($_SESSION['secret']) && true === $_SESSION['secret']) { ?>
<div>
<p>My secret content</p>
</div>
<?php } ?>

Categories