I've found a similar topic here: Using php to show a different header logo image if body class is home? but it doesn't seem to suite my code.
I am trying to display a larger version of the header on the homepage of a website and smaller version on all the other pages. I have this script:
if(strpos($_SERVER['REQUEST_URI'], "index.php") !== false) {
echo "<img src='images/header-full.png'/>";
} else {
echo "<img style='margin: 10px 15px 0;' src='images/header-small.png'/>";
}
but that only shows it if they click on the 'home' nav link and 'index.php' is located in the URL. How can I tell if the user is on the homepage even without the 'index.php' in the URL?
You certainly could do something like this:
if ($_SERVER['REQUEST_URI'] == "/" || $_SERVER['REQUEST_URI'] == "/index.php") {
...
Related
I would like to make navbar, in which background colour of one element would change if user would be on that subpage.
Code for checking if url contains chosen string:
<?php
$url = $_SERVER['REQUEST_URI'];
//echo ($url);
if (strpos($url, 'index.php') == true) {
//echo 'Current page contains index.php';
$atm = "#2275A8";
} else {
$atm = "#00427A";
}
?>
This is part from same file saved as .php file
<?php
header("Content-type: text/css");
?>
.ico1 {
background: <?php echo $atm; ?>;
}
If i put my "ckecking part" od code on index, it is working nicely; returning true or false, but for some reason it's not passing parameter into style file.
So now i put "checking part" into style file but, no mater on which subpage i am, its returning same result (always true even if 'index' is not part of url).
Any idea on how to deal with this? :)
Define classes with style you want in style.css file and change the class of element you want to change style of dynamically in php file like in the example below.
style.css
.ico1 {
background: color1;
}
.ico2 {
background: color2;
}
index.php (or the file containing navbar you want to change)
<?php
$url = $_SERVER['REQUEST_URI'];
if (strpos($url, 'index.php') == true) {
//echo 'Current page contains index.php';
$atm = "ico1";
} else {
$atm = "ico2";
}
?>
...
<div class="<?php echo $atm;?>"> // navbar
Why don't you have the two css classes and make a conditional in the element you want the class be applied:
CSS
.ico1-0 {
background: #2275A8;
}
.ico1-1 {
background: #00427A;
}
PHP
[...]
<div class="<?php echo (strpos($url, 'index.php') === true) ? 'ico1-0' : 'ico1-1'; ?>"></div>
[...]
strpos will never return true. Either it will return false or it return integer. So try like below.
if (strpos($url, 'index.php') !== false)
I'm having trouble getting a PHP script to work. It doesn't seem to be working accurately. I'm trying to make a script to only show HTML or Javascript on the homepage only. This script will be used for multiple things. I did google this, but most answers are for WP. This is vanilla PHP.
Here's what I used:
<!-- Add Only On The Homepage Or Else -->
<?php
$currentpage = $_SERVER['REQUEST_URI'];
if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) {
echo '
<header id="main-header" class="home">
';
}
else {
echo '
<header id="main-header">
';
}
?>
The results are that it shows the content of else, even though it's the homepage. I tested it on "/" and "/index.php".
I commonly use this snippet:
index.php
if (preg_match('/^m\.[^\/]+(\/(\?.*|index\.html(\?.*)?)?)?$/i', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])) {
readfile('index.html');
} else {
// Your framework bootstrapper or your own logic
}
I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});
I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.
How to write the following in PHP:
IF current page's name is pagex.php
THEN please load these additional CSS rules:
#DIVS { color:#FFF }
IF current page's name is anotherpage.php
THEN please load following CSS rules:
#DIVS { color: #000 }
like this:
<?php
if (basename(__FILE__) == 'pagex.php') {
echo '#DIVS { color:#FFF }';
} else if (basename(__FILE__) == 'anotherpage.php') {
echo '#DIVS { color:#000 }';
}
?>
PHP has some "magic constants" that you can inspect to get this information. Take a look at the ` __FILE__ constant.
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
So you can take this __FILE__ variable and execute the basename() function on it to get the file name. The basename() function returns the trailing name component of a path. Then you simply do a switch case to match the desired value -
$fileName = basename(__FILE__);
switch($fileName){
case 'pagex.php':
echo '<link .... src="some_stylesheet_file.css" />';
break;
case 'anotherpage.php':
echo '<link .... src="another_stylesheet_file.css" />';
break;
}
Your additional CSS rules can sit within those separate files.
Alternatively, if you don't want to split your css into multiple files, you can echo those specific rules into your page's head element like this -
echo '<style type="text/css">';
$fileName = basename(__FILE__);
switch($fileName){
case 'pagex.php':
echo '#DIVS { color:#FFF }';
break;
case 'anotherpage.php':
echo '#DIVS { color: #000 }';
break;
}
echo '</style>';
References -
basename()
php magic constants
You can just add in HTML head part one PHP if...else to load additional stylesheet according to page name.
<head>
<?php
if (basename(__FILE__) == 'one.php')
echo '<link .... src="style1.css" />';
elseif (basename(__FILE__) == 'two.php')
echo '<link ..... src="style2.css" />';
?>
</head>
you can use is_page() function of wordpress in a customize manner as it is working on regular php.code is:
<?php
$baseurl = 'http://www.example.com'; //set the base url of the site
$mypage1 = $baseurl."/pagex.php"; //add the rest of the url
$mypage2 = $baseurl."/anotherpage.php"; //add the rest of the url
$currentPage = $baseurl.$_SERVER['REQUEST_URI'];// this gets the current page url
if($currentPage==$mypage1) {
//do something with you style or whatever..
}
else if($currentPage==$mypage2)
{
//do something with you style or whatever..
}
?>
you have to change it according to your needs. i think it will help you.
happy coding!