View Full Version : PHP - if URL is Homepage, then do this...
espmartin
Oct 2nd 2007, 2:05 pm
Hello,
I need to ad a menu link to the homepage's navigation ONLY - and not have
that link sitewide.
How do I check via PHP if the page loaded is the homepage URL, and if so,
generate that link? If the page loaded is a sub-page, then that link should
NOT be there.
EXAMPLE:
Homepage + this link
Sub-page w/o linkI need this PHP code to be within an included sidebar.php file that sits in a
sub folder:
/includes/sidebar.phpCode examples would be very appreciated!
smatts9
Oct 2nd 2007, 2:31 pm
you can try using:
<?
$homepage = "/index.php";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo "links links links links";
}
?>
matthewrobertbell
Oct 2nd 2007, 2:32 pm
if ($_SERVER['PHP_SELF'] == 'index.php') {
echo 'link just for homepage';
}
espmartin
Oct 2nd 2007, 3:33 pm
you can try using:
<?
$homepage = "/index.php";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo "links links links links";
}
?>
I'm using this, but it does NOT work:
<!-- begin homepage ads -->
<?php
$homepage = "/index.php";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo '<!-- begin link-->';
echo '<li><a href="http://www.domain.com/" rel="external">Homepage only link</a></li>';
echo '<!-- end link -->';
}
?>
<!-- end homepage ads -->
It doesn't put that link on the page (or any page). Did I do something wrong?
smatts9
Oct 2nd 2007, 4:31 pm
is your homepage under index.php?
espmartin
Oct 2nd 2007, 4:37 pm
is your homepage under index.php?
Yes it is...When I tried that, the link does not show
smatts9
Oct 2nd 2007, 4:55 pm
try putting echo $currentpage; , put it right after the $currentpage =, line and goto your homepage to see what it displays.
espmartin
Oct 2nd 2007, 5:01 pm
Yes - that does echo the current location for whatever page I'm on. It shows
/ - /sub-folder/ - /etc/
So it does "know" the URL location, but it just is NOT echoing the link tag
espmartin
Oct 2nd 2007, 5:02 pm
I just found something...
On my local development PC (running Apache/PHP) - that bit of code DOES work!!!!
Just NOT working on my host...
smatts9
Oct 2nd 2007, 5:22 pm
does it echo index.php when you are on your homepage?
codyrockx
Oct 2nd 2007, 5:23 pm
Check the value of $currentpage by adding a debug line: echo $currentpage. If it does have a value there for when you visit the homepage try making $homepage that value assuming it's correctly returning the homepage for you. If it's not returning anything, it may be that your PHP installation doesn't support REQUEST_URI and you'll have to check your php settings to see what SERVER vars you can use. Check those out using phpinfo().
espmartin
Oct 2nd 2007, 5:29 pm
does it echo index.php when you are on your homepage?
On my local PC, yes it does. On my hosting account, no...
Check the value of $currentpage by adding a debug line: echo $currentpage. If it does have a value there for when you visit the homepage try making $homepage that value assuming it's correctly returning the homepage for you. If it's not returning anything, it may be that your PHP installation doesn't support REQUEST_URI and you'll have to check your php settings to see what SERVER vars you can use. Check those out using phpinfo().Got it:
And yes, it shows it's enabled:
REQUEST_URI /phpinfo.php
codyrockx
Oct 2nd 2007, 5:39 pm
I'm assuming then that you're homepage is at your domain rather than a page such as somedomain.com/. You'll have to check then if $currentpage = '' or index.php
espmartin
Oct 2nd 2007, 5:43 pm
I believe the syntax is correct, as it DOES work on my local apache/php server.
Just not on my web host...
So, when I use the code listed above, my local apache server serves the ad
on the homepage only, and sub-pages do NOT get it. On the hosting account
however, all I get is the echo of the current URI...
codyrockx
Oct 2nd 2007, 5:52 pm
No, definitely syntax is correct. If you're navigating to the homepage via the main domain and not /index.php it's not going to match. If the $currentpage that is being echoed is different from what you have set to $homepage, just change it to that.
espmartin
Oct 2nd 2007, 6:00 pm
Works!!!
Here's the total code:
<!-- begin homepage ads -->
<?php
$homepage = "/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
include('homepage-ads.php');
}
?>
<!-- end homepage ads -->
Must have been that /index.php - that I changed to just /
You guys rock!
espmartin
Oct 2nd 2007, 6:02 pm
Rep added to you guys!!!
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.