Detecting the current page from an included page - php

In my pages I have a top navigation bar.
I used to have this in the pages themselves but I've decided its better practice to have it in a seperate header file and then include this in every page.
The trouble is this has somewhat ruined my if server php self is whatever.php then class=current page on the nav bar.
Is there a way to detect the current page from within a header?

If I understand you correctly, you can use:
basename(__FILE__);
This will return the name current script that is being executed.
Without the extension:
substr(basename(__FILE__), 0, -4);

the way I found to do it was in my main page itself to set a variable
$current="whatever";
just before I run the header and then in the header check against the current variable

Related

php how to pass an anchor in the command line

I need to pass parameters and an anchor in a php program to make sure the cursor is positioned at the last place it was in the page when it returns.
program.php?ID=123&ID2=456#777
ID=123
ID2=456
html anchor is #777
Can someone tell me how to make this work? Since it isn't working the way I'm doing it now. Thanks
The browser will not send the anchor to web server.
So PHP can't get the anchor from url.
maybe you can use javascript to request program.php?ID=123&ID2=456&anchor=777, then you can get the anchor by $_GET['anchor'].
Sorry for my bad english (愒o愒).
If you want force the cursor to the position.
this code will redirect to http://website.com/page.php#anchor and the broswer will auto cursor the position.
<?php
header("location: http://website.com/page.php#anchor");
exit;
warning: take care about header function, there is the manual header function
If the current page is the same as the redirect page, above the code will infinite redirect and the browser will throw error. So you have to write some logic, make it redirect when you want. Or just write two different page, one to show content, other one redirect to content page with anchor.
But I still think it is better to do that by JavaScript.

PHP and web page displaying

I have searched all over the web trying to figure this out and am now trying to get a direct answer from some experienced users. I hope I can explain myself completely.
I know HTML and CSS and some PHP and Javascript, but no mean an expert. This is my questions:
When creating a website by hand (no Drupal, or Wordpress or predesigned templates), The first thing I do is create an index.php file that shows my HTML page layout. The second thing I do is create my links.inc.php file that will show all the links to my pages, ex: Home, About Us, Contact Us. Now on the index.php page I create php include files for the header, footer, and link pages. (these would read header.inc.php, footer.inc.php, links.inc.php) Now here is where I am trying to figure if there is an easier way to do the next step.
My normal steps would next to be to create a home.inc.php, aboutus.inc.php, contactus.inc.php files which will have all the "content" I want shown for each page.
I would then create a duplicate of the index.php and create aboutus.php where I would use the php include function to add the aboutus.inc.php into the "main content" area I would want this information displayed at. Then I would create anther duplicate of the index.php and name it contactus.php and "include" the contactus.inc.php file.
Is there any way to use the index.php file and have all the inc.php files on that page? For instance,
<div id="main">
<?php
include ("home.inc.php");
include ("aboutus.inc.php");
include ("contactus.inc.php")
?>
</div>
Obviously this does not work they way I have it laid out above, it shows all the pages at the same time instead of only showing the one page that is clicked on from the menu. Any suggestions? Is there a different way or am I doing it correctly with creating multiple pages?
Thank you for any help and I hope I was clear, if not I can try to explain a different way.
My suggestion is to include files conditionally, based on a variable that defines the current page.
For example, given the following navigation:
Home
About Us
Contact Us
Configure your index.php file to include external files, something like this:
// determine the requested page, default to the home page
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// check if the requested include file exists
$include_file = is_file($page.'.inc.php') ? $page.'.inc.php' : false;
// if the requested include file exists, include it
if ($include_file) {
include $include_file;
}
Feel free to adjust the logic. For example, if a $page value is not recognized as a valid page on your site, you may want to show a 404 page, default to the "home" page, etc.
Edit
If your include files are in a different directory, you'll need to provide the correct path:
// define the path to includes
$path = 'inc/';
// check if the requested include file exists
$include_file = is_file($path.$page.'.inc.php') ? $path.$page.'.inc.php' : false;
You could send a variable to PHP index.php?action=home; then, inside index make some verifications
if($action=="home") {include index.inc.php; }
else if ($action=="contact") {include contact.inc.php }
and so on.

Dynamic site, using include to get content in specific div , how?

I want to use php to easily maintain my website, but I simply can't figure out the language - I've found some tuts online, and some other questions here, but none help me.
I've divided my site into some .php files, header/footer and such - And using
works fine..
Now I want the content of my site, to update according to which menu I click on at my site.
http://dawtano.com/pp/
If I click on "about" I want the "Hello World" to open inside my content div, but I can't get the right php code to do it.
I think you should do this---
Note: This will only work if the CSS styling are on the current directory! ()
<div>
<?php
$html_page = implode('', file('http://dawtano.com/pp/'));
echo $html;
?>
</div>
Hope this helps!
well currently your links are taking you to a separate page entirely. So why not just code it so that your include file is specific to the page. i.e, on about.php, use something like
include 'about_content.php
in your contetnt div.
If you're looking for your content to load dynamically into the content div you'll need to look into using ajax to fetch the content pages.
One popular way to construct the site is to have a single php script which displays content based upon a $_GET variable like 'page' or 'content', and then make the link as:
'http://dawtano.com/pp/index.php?page=helloworldcontent'
Using this method, you would need to check if the variable ($_GET['page']) is set using isset(), and then make sure the string is safe... as anybody with a browser could just type in some mumbo-magic script and hijack your site:
'http://dawtano.com/pp/index.php?page=somecleaverlycraftedhax'
Once it exists and is safe, add the '.php' to the file name and include that file... if it exists! If it doesn't exist, then you will need some code to handle that, probably by displaying a 'File not Found' message, or redirecting home, or something.
I prefer not to do this because it is a pain to make safe, and I feel like it is pretty ugly. What I do instead is put all the header/footer/navbar/title bar scripts into seperate 'display' functions, and put them in another file.
Then include this file with the function definitions, and call all the 'display' functions to set up the page. So every php script in your site might look like:
<?php
include 'html_display_functions.php';
/* put lines here to parse $_GET and $_POST, session_start()/$_SESSION, etc... */
print_html_pre_content();
print '<p>Hello, world!</p>';
print_html_post_content();
?>
Since every script will have this structure, you can just create a template file once. When you want to create a new page for your site, copy the template, rename the copy to the php filename you want, and add content between the two print functions.
You also keep the ability to modify the header/footer/navbar/title bar for the whole site in a central location, namely the included file with the functions.
You might be looking for some sort of Template Engine which allows you to create your pages out of variable parts. You could have a look at TBS, which is more or less what is suggested by the name. But there is a whole lot more engines out there which could do the job.
If that's already too much over the top, maybe Apache SSI (Server Side Includes) are a try for you.
A little suggestion from my side, I am often using Apaches mod_rewrite in connection with a single controller.php file. Apaches mod_rewrite will then send all request to the controller.php which will fetch the appropriate page parts for the requested page using TBS and return the respective page. So you have the controll of the page in one location only.
To your original question about.php could look like:
<?php
include('header.php');
?>
// original page content as html for about.php
// assuming header ends with the starting div <div> where you like the content to appear
// and footer starts with the closing div </div>
// if you need variable content here, simply use <?php echo $your_variable ?>
<?php
include('footer.php');
?>
The best way would be to use a switch statement:
http://php.net/manual/en/control-structures.switch.php
Something like this:
<?php
include("header.php");
$page = $_GET['page'];
switch($page)
{
case "about":
include "about.php";
break;
case "faq":
include "faq.php";
break;
case "help":
include "help.php";
break;
default:
include "home.php";
}
include("footer.php);
?>
Then just make all of your links look like this:
http://www.example.com/index.php?page=home
Just replace home with the correct page.

Code to detect current page

Is there a php or javascript code that can detect the current user's page and then add <a class="active"> to an item in a ul (my menu). I include my menu in my pages with PHP include so making change is easy; I only have to edit it once. But, with that method, I can't individually set each page to have a class="active". How can I do this?
You several options, e.g.,
The part that handles navigations can read the request URI directly. This can be done by reading $_SERVER['REQUEST_URI'] (don't forget this may include the query string).
At some point, you must know what page you're on, because you decide which content you display based on that. You can define a function that handles the navigation markup and pass it the name of the current page so that it knows which one it is.
If I'm understanding your question correctly, what I usually do is set a variable before I include the header, like
$current = "home";
And then in the header I'd have an if statement in each link
<a href="/home" <?php if ( $current == "home" ) { echo "class='active'" } ?>>Home</a>
Could be ways to improve it, but it's simple if your menu isn't too big.
In PHP, you can look at the value of $_SERVER['REQUEST_URI'].
In JavaScript, you can examine window.location.

PHP Header Location Change of Parent Frame

I have an iFrame that does some background processing. When this processing is complete I would like to re-direct the user to another page, but the header change code is only affecting the embedded iFrame. Is there a way to target the main window?
I have seen the deprecated Meta redirect have a target attribute, but I don't know how widely it is supported.
In Javascript:
top.location.href = "resultpage.htm";
Works only if the top frame is on the same domain as the emitting page.
For a solution that works across domains and without Javascript, the following would work:
Continue
Use JavaScript to track content of frame, if content change, redirect browser :)
We can use javascript like this:
target.window.location='locationpage.php';
parent.window.location='index.php';
For me this always works without fail, have tried many of the others but there always seems to be some sort of issue....and it does not matter if headers have been sent etc.....
<?php
echo "<script>window.location = 'http://www.google.com'</script>";
?>
Remember this goes at the very bottom

Categories