Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php include question

Featured Replies

Hey everyone!

 

I have a site that is put together with php includes. The header is inserted into the document with an include, the footer and and the copyright area are as well. The main body is the only area that is on the page.

 

Now, part of header.inc.php is the text buttons. If I were doing this in a straight .html document. I would change the styling of the text button when the visitor is on that page so show them where they are. But with a php include, the button bar is generic. How can I have it so that the PHP include figures out the visitor is on index.php and hilights the Home button and so on and so forth?

 

Is this easy to do? Please advise.

 

Thanks!

 

-Seth 8)

Hello

 

You can work out which page your on using:

 

$pageName = basename($_SERVER['SCRIPT_NAME']);

 

If your using <ul><li>'s you can try :

 

<ul>

<li><a href="index.php" <?php if($pageName == "index.php"){ echo 'id="pageselected"';} ?>>Welcome</a></li>

ect......

</ul>

 

Rob

  • Author

I don't think I quite understand Rob? I'm not using lists. Just text links. Could you give me an example of this one?

 

$pageName = basename($_SERVER['SCRIPT_NAME']);

 

How would I write this out?

 

What is SCRIPT_NAME? What script?

 

is $pageName the url? like index.php? And if it is then how do I tell it what style it should be if it is?

 

Does that make any sense?

Hello

 

$_SERVER['SCRIPT_NAME'] holds the full path and filename of the current script eg /root/phpstuff/siteone/index.php. The values in the $_SERVER array are created by the server automatically.

 

basename chops off the path + returns the page name - index.php

 

so :

 

$pageName = basename($_SERVER['SCRIPT_NAME']); - $pageName will now hold the name of the current page.

 

 

You can use php if statements to show/hide html code eg

 

<?php

$pageName = basename($_SERVER['SCRIPT_NAME']);

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

<title>Index</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body>

<h1>Some heading or other</h1>

 

<?php if($pageName == "index.php") { ?>

<h2>This is index.php</h2>

<?php } else { ?>

<h2>This page is not index.php</h2>

<?php } ?>

 

</body>

</html>

 

You can use this with the id selector. If you have an id called highlight, which is used to highlight the current page :

 

<p><a href="index.php" <?php if($pageName == "index.php"){echo 'id="highlight"';} ?> >Home </a></p>

<p><a href="about.php" <?php if($pageName == "about.php"){echo 'id="highlight"';} ?> >About Us</a></p>

 

Hope that helps

 

Rob

  • Author

Thanks Mate! 8)

  • Author

Question what is 'SCRIPT_NAME' what do I put there?

Hello

 

'SCRIPT_NAME' is the array key that accesses the current scripts path. You just use 'SCRIPT_NAME' , its server created - so its already there.

 

http://uk2.php.net/manual/en/reserved.variables.server.php has some other $_SERVER array keys.

 

You can test what you get by - copy/paste the next bit and save as testing.php + test in brower.

 

<?php

 

echo "{$_SERVER['SCRIPT_NAME']}<br />";

$thisPage = basename($_SERVER['SCRIPT_NAME']);

echo "{$thisPage}";

 

?>

 

Try replacing 'SCRIPT_NAME' with some of the other $_SERVER array key's + see what you get.

 

Rob

  • Author

great now it works thanks so much!

great now it works thanks so much!

Glad I could help :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.