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>";
}
Related
In OpenCart 2, I am editing the appearance/php of the header only in the "success"/"thank you" page (catalog/view/theme/*/template/common/success.tpl).
So, in catalog/view/theme/*/template/common/header.tpl I want to do something like:
if( $is_thank_you_page ){
echo "stuff";
// bonus: I wanted to get the order email but maybe it should be a different post
}
But how can I check in the header.tpl if it is the "success"/"thank you" page?
I tried setting a variable in success.tpl before printing the header with no results.
You could try something like this (go about it based on your URL):
<?php
$parameters = explode('/', $_SERVER['REQUEST_URI']);
if(end($parameters) === 'success.tpl'){
//the condition with $parameters depends on the exact look of your URL
//you could also access an index directly
}
Basically, it takes the REQUEST_URI (part after the domain), splits it around the / symbols and then checks if it ends with success.tpl
You could also make a switch for the end($parameters) instead of the if.
I don't know opencart structure, but if this value never change you can try with strpos/stripos, something like:
if(stripos($var_with_page_title, 'thank you') !== false) {
do_something();
}
If you want you detect checkout/success page in you header, do following:
open catalog/controller/common/header.php
find
// Menu
$this->load->model('catalog/category');
Add before
// success page checking
$data['success'] = '';
if (isset($this->request->get['route']) && $this->request->get['route'] == 'checkout/success') {
$data['success'] = true;
}
// looking for email from the order
$data['success_email'] = '';
if ($this->customer->isLogged()) {
$data['success_email'] = $customer_info['email'];
} elseif (isset(this->session->data['guest']['email'])) {
$data['success_email'] = $this->session->data['guest']['email'];
}
Now in catalog/view/theme/YOUR_THEME/template/common/header.tpl
add anywhere you like
<?php if ($success) { ?>
//do something
<?php if ($success_email) { ?><?php echo $success_email; ?><?php } ?>
<?php } ?>
With bonus email
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)
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;
}
?>
Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
I'd like to make subpages within the same page, using if isset(get) to show the subpages.
I have a page: page.php and a subpage: page.php?do=userList and I'd like to make other subpage using the same URL: page.php?do=userList&id=123
This is my code (page.php):
if(empty($_GET)) {
?>
User List
<?php
}
if (isset($_GET['do']) && $_GET['do'] == 'userList') {
// the user list
echo '' . $row['name'] . '';
}
if (isset($_GET['id'])) { // ???
// user info
}
Can I use that URL but showing only the content of: "if (isset($_GET['id']))"??
If is set $_GET['id'] don't show $_GET['do']... :P
Just add && !isset($_GET['id']) to your second if statement.