Jump to content

Recommended Posts

This may sound funny, but I'm not sure what I was trying to get this code to do...

        <!-- Find Current Page Name -->
        <?php
          $page = basename($_SERVER['REQUEST_URI']);
				echo '<p>$page = ' . $page . '</p>';
//          if ($page == '') {
          if (($page == '/')||($page == '')||($page == 'index.php')){
            $page = "index.php";
					echo '<p>You are on the Home Page.</p>';
          }else{
					echo '<p>You are on Some Other Page.</p>';
				}

 

What I mean by that is I'm not understanding why I chose...

          if ($page == '') {

 

and then...

          if (($page == '/')||($page == '')){

 

and then...

          if (($page == '/')||($page == '')||($page == 'index.php')){

 

 

Why would I get those different results from basename($_SERVER['REQUEST_URI'])??

 

And what should I expect locally on my Laptop with a Virtual Host set up (e.g. "http://local.debbie/index.php") versus using a Test Site (e.g. "http://www.doubledee.byethost2.com/") versus a real site (e.g. "www.debbie.com")?!

 

Hope I'm making some sense here...

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/
Share on other sites

Why would I get those different results from basename($_SERVER['REQUEST_URI'])??

You would get different results in different cases :) You may call main page in different ways:

http://your-site.com/

http://your-site.com

http://your-site.com/index.php

 

These are 3 cases that you test: '/', empty or 'index.php'.

 

And what should I expect locally on my Laptop with a Virtual Host set up (e.g. "http://local.debbie/index.php") versus using a Test Site (e.g. "http://www.doubledee.byethost2.com/") versus a real site (e.g. "www.debbie.com")?!

It's up to you: what page name would you enter at the end of URI is what you would expect to get in the script.

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1295179
Share on other sites

basename() will never return a slash ($page=="/" is impossible) and the REQUEST_URI will never be empty.

 

If $page=="" then the URI looks like a directory - it has a trailing slash. (That, of course, says nothing about whether the URI actually is for a directory.)

 

And the hostname has no bearing on any of this.

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1295424
Share on other sites

basename() will never return a slash ($page=="/" is impossible) and the REQUEST_URI will never be empty.

 

If $page=="" then the URI looks like a directory - it has a trailing slash. (That, of course, says nothing about whether the URI actually is for a directory.)

 

And the hostname has no bearing on any of this.

 

I don't follow you.

 

So how are you saying my code should look?

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1295617
Share on other sites

It should look like whatever it needs to look like to do what you want. Something involving the REQUEST_FILENAME, REQUEST_URI, and/or basename() sounds appropriate. But I don't know what the code wants or why so all I can do is guess.

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1295893
Share on other sites

It should look like whatever it needs to look like to do what you want. Something involving the REQUEST_FILENAME, REQUEST_URI, and/or basename() sounds appropriate. But I don't know what the code wants or why so all I can do is guess.

 

Like I said, I just need a generic way of determining if I am on the Home Page or any other page.

 

This is what I have...

        <!-- Find Current Page Name -->
        <?php
          $page = basename($_SERVER['REQUEST_URI']);
				echo '<p>$page = ' . $page . '</p>';
//          if ($page == '') {
//          if (($page == '/')||($page == '')){
          if (($page == '/')||($page == '')||($page == 'index.php')){
            $page = "index.php";
		echo '<p>You are on the Home Page.</p>';
          }else{
		echo '<p>You are on Some Other Page.</p>';
  	  }

        ?>

 

 

A lot of what I'm confused about is How, When, and Where a page resolves in the URL.

 

For instance, when would I need to check for '' versus '/' versus 'index.php' versus something else?

 

Is there some standard way of saying, "Hey, you are on your Home Page!"??

 

My code needs to work regardless of whether I am on my Virtual Host or on my website online or whatever...

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296015
Share on other sites

For instance, when would I need to check for '' versus '/' versus 'index.php' versus something else?

 

'/' should never come up, since it represents a directory.  All you would need to check for would be '' or 'index.php' but that isn't going to necessarily give you whether your on the home page or not.  If you have any sub directories with an index.php file, it would return true for those as well.

 

You can just check if $_SERVER['REQUEST_URI'] equals either '/' or '/index.php', or any other url which would represent your home page.  You could also define() a constant that tells you which page your on and check for that.

 

Why do you need to know what page your on?

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296016
Share on other sites

For instance, when would I need to check for '' versus '/' versus 'index.php' versus something else?

 

'/' should never come up, since it represents a directory.

 

A-ha!

 

But when I go to "http://www.doubledee.byethost2.com" and I copy what is in the URL and paste it I get "http://www.doubledee.byethost2.com/" so that is exactly what I'm talking about?!

 

 

All you would need to check for would be '' or 'index.php' but that isn't going to necessarily give you whether your on the home page or not.  If you have any sub directories with an index.php file, it would return true for those as well.

 

True, but I only have one "index.php"

 

If you have a more universal approach, then I'm all ears!  ;)

 

 

You can just check if $_SERVER['REQUEST_URI'] equals either '/' or '/index.php', or any other url which would represent your home page.

 

Did you look at my code above?

 

 

You could also define() a constant that tells you which page your on and check for that.
 

 

Doesn't my code sorta do that?

 

 

Why do you need to know what page your on?

 

Well, I need to know if I am on the "Home Page" or another non-"Home Page" page because I am using one set of HTML in my included Header when on the Home Page and another set of HTML for all other pages.  So I need my php in my included Header to be able to determine where it is at and then echo the appropriate HTML.

 

Follow me?

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296020
Share on other sites

But when I go to "http://www.doubledee.byethost2.com" and I copy what is in the URL and paste it I get "http://www.doubledee.byethost2.com/" so that is exactly what I'm talking about?!

Paste it where?  $_SERVER['REQUEST_URI'] shouldn't contain the full url anyway, though I guess it might be possible.

 

True, but I only have one "index.php"

If you have a more universal approach, then I'm all ears!  ;)

Now maybe, what about a year from now?  Things change.

 

Did you look at my code above?

Yes, your checking the basename, not the variable directly.  I mean code like:

if ($_SERVER['REQUEST_URI']=='/' || $_SERVER['REQUEST_URI'] == '/index.php'){

 

Doesn't my code sorta do that?

Not really.  What I am talking about is on your index.php page, you would have code such as:

<?php
define('CURRENT_PAGE', 'home');
//...

 

Then in your file where you need to test if your on the home page or not, use

<?php
if (defined('CURRENT_PAGE') && CURRENT_PAGE=='home'){
// on home page
}
else {
  //not on home page
}

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296024
Share on other sites

But when I go to "http://www.doubledee.byethost2.com" and I copy what is in the URL and paste it I get "http://www.doubledee.byethost2.com/" so that is exactly what I'm talking about?!

 

Paste it where?

 

If I click on my ++DoubleDee bookmark it takes me to...

 

http://www.doubledee.byethost2.com

 

That is what I see in the Address Bar.

 

If I click in the Address Bar and copy "http://www.doubledee.byethost2.com" and then I paste whatever I copied somewhere (e.g. TextEdit) then I get "http://www.doubledee.byethost2.com/"

 

Somehow that "/" must be in the original Address bar but I just don't see it?!

 

So if my code is or is not checking for that hidden "/" then it might not work.

 

THUS my request for help and a way to ensure that my code looks at all combinations.

 

Follow me know?!

 

 

Doesn't my code sorta do that?

Not really.  What I am talking about is on your index.php page, you would have code such as:

<?php
define('CURRENT_PAGE', 'home');
//...

 

Then in your file where you need to test if your on the home page or not, use

<?php
if (defined('CURRENT_PAGE') && CURRENT_PAGE=='home'){
// on home page
}
else {
  //not on home page
}

 

Nah, that seems too manual and too crude to me.

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296033
Share on other sites

Not really.  What I am talking about is on your index.php page, you would have code such as:

<?php
define('CURRENT_PAGE', 'home');
//...

 

Then in your file where you need to test if your on the home page or not, use

<?php
if (defined('CURRENT_PAGE') && CURRENT_PAGE=='home'){
// on home page
}
else {
  //not on home page
}

 

Nah, that seems too manual and too crude to me.

 

 

Debbie

 

Too manual? Well generally configuration options are...

 

This is the best way to make sure your URL's are correct. Things may vary in server configurations and such, and if you need to dynamically create URL's for example then you have a way to control them.

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296035
Share on other sites

The REQUEST_URI will always start with a slash, regardless of whether your browser shows it to you in the address bar or not.

 

What kicken said about checking for / or /index.php is 100% correct.

 

Okay, I think that answers my question.

 

Thanks!

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296037
Share on other sites

The REQUEST_URI will always start with a slash, regardless of whether your browser shows it to you in the address bar or not.

 

What kicken said about checking for / or /index.php is 100% correct.

 

But will it remove a TRAILING SLASH like "index.php/" or "www.debbie.com/"

 

The Manual doesn't say...

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296040
Share on other sites

Will basename()? Perhaps you just aren't understanding what the function does?

 

If you give it something that looks like a file path, it will give you the name of the file without the path. If the path is

http://www.debbie.com/index.php/foo

then you will get back "foo".

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296352
Share on other sites

Like I said, I just need a generic way of determining if I am on the Home Page or any other page.

 

I use

// Start defining the URL...
// URL is http:// plus the host name plus the current directory:
$url = dirname($_SERVER['PHP_SELF']);
// Remove any trailing slashes:
$url = rtrim($url, '/\\');
$file = basename($_SERVER['PHP_SELF']);
$file = "/$file";
$url = $url.$file;

 

You can echo $url or $file or both and see what they give you on different pages

 

I don't know what you want but I then use a switch function to determine the info page I needs

//$folder = ''; when online <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
switch ($url) 
{
case "$folder/index.php":
$page_title = 'Xyz';
$h1 = 'Xyz';
$h2 = 'xxx';
$description = page description';
$keywords = 'keywords';
break;

// Default is to include the main page.
// Note no robots as default
default:
$page_title = 'Xyz';
$h1 = 'Xyz';
$h2 = 'xxx';
$description = page description';
$keywords = 'keywords';
break;
}

 

I use this nto determine the folder

 

if (stristr($_SERVER['HTTP_HOST'], 'local') || (substr($_SERVER['HTTP_HOST'], 0,7) == '127.0.0')) {

$local = TRUE;

$home = 'http://localhost/local-folder'; // No backslash, it's added in switch

$folder = '/local-folder';

$debug = TRUE;

} else {

$local = FALSE;

$home = 'http://www.website.com';

$folder = ''; //$folder = ''; when online

}

 

 

I throw the $home in links like <a href="<?php echo "$home"; ?>/index.php">Home</a> that way they work on my localhost & online

 

$folder is used in the switch file

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296397
Share on other sites

PHP_SELF is insecure. Use REQUEST_FILENAME instead.

 

I just thought about that as my dinner was cooking. I use PHP_SELF on pages that don't need really need security(I hope), no login or out or entering or editing info etc.  I'm going to ck into REQUEST_FILENAME, thanks

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296414
Share on other sites

PHP_SELF is insecure. Use REQUEST_FILENAME instead.

 

It really depends on how you use PHP_SELF.

 

It's more that PHP_SELF is determined with client input, rather than entirely by the server. If you plan on using it, be careful and make sure you sanitize it before you output or insert into a database.

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296415
Share on other sites

I use PHP_SELF on pages that don't need really need security(I hope), no login or out or entering or editing info etc.

That's not what I mean. The value won't necessarily be what you think it is.

 

Of the many links I could post, here's two random ones:

$_SERVER['PHP_SELF'] can not be trusted, but there are safe alternatives

Don't trust PHP_SELF

 

[edit] Okay, more of a "not trustworthy" than "insecure".

Link to comment
https://forums.phpfreaks.com/topic/252642-determine-home-page/#findComment-1296417
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.