I am trying to find the best way to have a DIV be hidden if the url is something. For example I have 2 sites using the same template but only 1 of those sites I want to display something.
So site A is domain.com
Site B is site.domain.com
I want it so if site.domain.com is where people are at then do not show DIV ID="hide". I also need it to have this work for not just that specific URL but for anything that comes after it so site.domain.com/aboutus.php, site.domain.com/contact.php etc....
I would like to do this with PHP or JS.
Just check HTTP_HOST:
<?php
if($_SERVER['HTTP_HOST'] != 'site.domain.com'){
echo '<div>contents</div>'; // display div, he is not on site.domain.com
}
?>
You can probably do something like:
<php if($_SERVER['HTTP_HOST'] == 'example.com'): ?>
<div>This is example.com</div>
<?php else: ?>
<div>This is NOT example.com</div>
<?php endif; ?>
But I think you'd really be better off creating some sort of site1_settings.php that gets included on every page in one site, and then site2_settings.php that gets included in the second. For one thing, this will make it much easier to test your code locally.
I usually use strpos for this.
if(strpos($_SERVER["HTTP_HOST"], "site") !== FALSE)
{
// The user is on site.domain.com
}
else
{
// The user is on domain.com
}
Here would be my javascript implementation. Note: jQuery
if(window.location.indexOf("textinurl"))
{
$('#DivId').hide();
}
Related
So on my index page - when it loads I wanted PHP to check if the user was logged in. If the user was logged in it would show the contents of the webpage but if not it would show the login page for the user.
This is the PHP for the check.
<?php
if ($_SESSION['loggedin'] == true) {
echo "contents of the page";
}
else{
echo "the sign up form";
}
?>
My problem now is that right now I have to put all my HTML in an echo statement like this -
echo '<form>inputs</form>';
and that turn into a bunch of yellow text that I can't easily edit because it's being recognized as plain text by the editor(Sublime Text 3)
Maybe there is a way to get the HTML from a different file and link it into the echo?
What do ya'll think? What's the best route to take? Thanks!
You can embed normal HTML after closing PHP tags like this
<?php if(check if user is logged in): ?>
<pure html to show content of page/>
<?php else: ?>
<pure html to show signup form />
<?php endif; ?>
I hope this will help you
You just have to break out of the PHP tag. You can even do it inside a conditional:
if (condition) { ?>
True stuff!
<?php } else { ?>
False stuff...
<?php }
Given the path to your index.html, you could do:
<?php include_once("path/to/index.html"); ?>
You should really consider using php MVC frameworks.
Number one, for this would be Laravel, but you can choose from many others
Symphony, PHALCON, Zend, CodeIgniter.
And regards to IDE I would really recommend PHPStorm, but you can also use others like Eclipse, Netbeans, Aptana Studio....
I have a template in my (wordpress) child theme folder for a custom post type that I created. It looks like this:
<head>
…
<style>
…
</style>
<script>
…
</script>
</head>
<header>
…
</header>
<?php
…
?>
<div id="content">
<?php xx_page_header($post->ID); ?>
…
</div>
<?php get_footer(); ?>
This is exactly how it is structured. I put it because I am php beginner and if someone is willing to help me, at all, I will most of all need exact information where exactly to put code snippets (which in the end will look simple and small, I guess).
I have 4 pages that are based on this template page. All of them working well.
Here is what I have to add though:
The pages succeed each other containing input forms etc. and are clicked through by passing the forms, sort of a filtering process.
How can I put in front of the template a referer check? So that the pages keep on working their way BUT if someone would copy one of the links and paste it into another browser window he would be redirected always to the first page of them instead of seeing the page of the actual link.
I have made some researches as good as I can and found some pieces I would like to use. As is for:
1) the referer part:
<?php
$ref=getenv("HTTP_REFERER");
$url='url_redirect';
if($ref!='referer_url'){
header('Location: '.$url);
}
?>
2) a regex to make it work for all pages that are using the template:
ref.match(/^http?:\/\/([^\/]+\.)?mywebpage\.com(\/|$)/i)
Let’s say the (filtering) start page is: www.mywebpage.com/startpage/
How do I have to combine those snippets and where to put it (I guess at the beginning of the template)?
I would like to avoid cookies. (using the referer)
Technically, you could do something similar to the code below. Each of your pages after the start page must have unique urls and a token that is passed along on every page. You need to place this code at the very top of your template.
Something similar:
<?php
$url='url_redirect';
if((is_page('template2') || is_page('template3') || is_page('template4')) && $_POST['token'] == '') {
header('Location: '.$url);
}
?>
I have noticed that in a couple of sites they can run the whole site from just the index.php, I also noticed the same effect in phpmyadmin to some extent.
An example of a site that does this is Try Open School, it is a demo site, and you can log in with (admin as password and admin as username, or teacher, teacher, or student, student). If you notice all that changes about the URL is the Query String.
I looked through the site and all I can Image is a whole lot of codes on just one page, But I do not think this is the case. I tried mine and I did something like this..
<?php
if($_GET['ref'] == 'home')
{
require_once("home.php");
}
elseif($_GET['ref'] == 'profile')
{
require_once("profile.php");
}
?>
I used the URL Query String to make reference to the different pages and to make other logical decisions.
I did a whole lot of this on the page, and the page contained several hundreds of if else statements. I do not know if I am doing what they did on their site.
I love the concept and would like to implement that kind of stuff on my own site.
I would appreciate all help on how to go about this. Thanks
If $_GET['ref'] is the only parameter for selecting page. a php switch, would probably be a better solution.
If you want to get fewer lines of code in your index file, you could put the switch in a separete php file!
Like this (index.php)
<?php
echo "<div id=header>Someinput</div>
<div id=menu>Some menu input</div>
<div id=site>";
require("switch.php");
echo "</div>
<div id=footer>Some footer input</div>";
?>
Then (switch.php):
<?php
switch($_GET['ref']){
case "page1":
include("page1.php");
break 1;
case "page2":
include("page2.php");
break 1;
default:
include("404.php");
}
?>
I have a custom CMS here entitled phpVMS, and I want to exclude a piece of code, a banner for a single page. phpVMS is steered using templates, for instance, the main template that codes the general layout for all pages is entitled layout.tpl. So, like I said, this displays whatever is in the template, on all of the pages. I have however created a special control panel, and therefore require to exclude the banner, because it slightly destroys the theme of it. Is there any PHP code that excludes a piece of code on a single site? I need to remove a single div...
<div id="slideshow"></div>
...on a single page.
Basically, I could create a new template but this is a very long winded and unefficient way within this CMS, and the final result isn't that great - because I can't reinclude the mainbox div which is the box defining the content on the centre white bit of the theme - it's already in the layout.tpl.
I hope you can somehow help me, hope I've included enough information there.
Thanks.
I don't think you can do what you're asking in PHP, but you might be able to do this on the client-side, by either hiding the div (CSS display:none) or by removing it with JavaScript. You might be able to do something like:
<?php
include("layout.tpi");
if (condition)
{
// Javascript:
echo "<script>document.getElementById('slideshow').style.display = 'none';</script>";
// OR jQuery:
echo "<script>$('#slideshow').hide();</script>";
}
?>
If you use a variable to determine you don't want to include the div, you could do this:
<?php if ($include) { ?>
<div id="slideshow"></div>
<?php } ?>
OR
<?php
if (!$include)
echo "<!--";
?>
<div id="slideshow"></div>
<?php
if (!$include)
echo "-->";
?>
EDIT: Obviously, there is no good reason to use the second method. The second method will only comment out the HTML so it will still show up in the source.
I'm not sure if this is what you are looking for, but seems simple
<?
$template = true;
if($template) {
?>
<div id="slideshow"></div>
<?
}
?>
On the template, you could have some code that reads:
if($_SERVER['PHP_SELF'] == /*control panel file*/) {
//exclude
}else{
//include
}
Desperately hoping someone can assist with this. I'm a novice with php. I try and self teach myself through tutorials and I've searched high and low to no avail.
Basically I'm looking to implement an "If index.php page, show foo, if not on index.php page, show bar"
Any ideas?
I hope I explain this well enough...
index.php includes a sidebar:
require_once('./cache/templates/sidebar.php');
Every subsequent page is built uses what's defined in this index.php file, meaning the sidebar.php is a must.
I'm wanting to edit sidebar.php to contain an advert which displays solely on the index page.
At the moment, when I edit sidebar.php, so for instance, to display the letter "B", it will display on the homepage, and every other page like so;
Index Page: http://img.photobucket.com/albums/v684/nilsatis/1stack.jpg
Every other page: http://img.photobucket.com/albums/v684/nilsatis/2stack.jpg
How can I dictate one area of an included file to display on one page but exclude showing on others?
Any assistance would be very appreciated.
[Edit] This is the website in question: www.grandoldteam.com . You can see where I have the text "Test" - this was entered in sidebar.php. I'd like this text (future advert) to feature only on the index page, nowhere else.
[Edit 2] This is the point in which sidebar.php is called in the index.php file;
<p class="page_desc">'.$PAGE['subtitle'].'</p>
' );
}
if (isset($url[1])) require_once('./cache/html/'.$url[0].'/'.$url[1].'.php');
else require_once('./cache/html/'.$url[0].'.php');
}
}
require_once('./cache/templates/sidebar.php');
}
require_once('./cache/templates/footer.php');
And this is the but in which I can edit sidebar.php to display wanted text;
<div class="clear"></div>
test
</div>
<p>
</div>
To do it the way you want, use the $_SERVER superglobal. The script's name is found in more than one place.
if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) // index page...
Another option is to have index.php set some variable like $show_ad before including the side bar, and the side bar page can check that variable.
I would not recommend to retrieve the name of the caller script, because several pages can have the same name in different folders, and also because you may want to change page names in the future.
Use a global variable or a constant that says which page is the caller.
index.php:
<?php
$GLOBALS['caller_page'] = 'index';
require_once('./cache/templates/sidebar.php');
...
sidebar.php:
<?php
...
if ( isset($GLOBALS['caller_page']) && ($GLOBALS['caller_page']=='index') ) {
... // special action for the index page
}
...
Try this instead,
Create a session on the page where you only want to show "foo".
Then do this
if ($_SESSION['valid']) {
//if the session is present, then show
}
else {
//if not,
}
This is not only a better way of going about it as what happens if your filenames get changed? This way it doesn't matter as it is checking against a session, not something that could change :)
How to work it out:
At the top sidebar.php, add the line:
print_r($_SERVER);
This will show you the full contents of the $_SERVER variable. From that, you should see a number of candidates that could be used, as well as other things that may be useful to you at some point.
Once you've decided on what to use, you will need to check whether it includes the string index.php. A good way to do that would be to use:
if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') !== false) {
}
This idiomatic line checks to see whether the position of the string index.php in the script name isn't false, that is, it is a value.
Test the value
$_SERVER[ 'REQUEST_URI' ]
against the literal
'/index.php'.
If it is identical. you index.php is executing.
all of those answers are great, but i provide a different practice. It's not better, but in my feeling it's more comfortable.
when you do this:
require_once('./cache/templates/sidebar.php');
on index.php, do this instead:
require_once('./cache/templates/sidebar.php?ad=1');
now, on sidebar.php, add this:
if(isset($_GET['ad'])&& $_GET['ad']=='1')
// display ad
else
//don't display ad
EDIT:
you want working code, fine...
lets say your ad is actually the image of stackoverflow logo:
now on sidebar.php you'll have somthing like:
<?php
....
if(isset($_GET['ad'])&& $_GET['ad']=='1')
{
?>
<div>
<a href="http://stackoverflow.com">
<img src="http://blog.vicompany.nl/wp-content/uploads/2011/04/stackoverflow-logo-250.png" alt="ad"/>
</a>
</div>
<?php
}
else
{
}
....
?>