LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Question: how to make include(); depending on URL? (https://www.linuxquestions.org/questions/programming-9/php-question-how-to-make-include-%3B-depending-on-url-89471/)

linuxfond 09-05-2003 01:27 AM

PHP Question: how to make include(); depending on URL?
 
PHP programming question:

Is it possible to tell a PHP page to include one or another file depending on URL location?

For example:

if
URL is http://mysite.com/something.php,
then
include(file_for_something.php);
if URL is http://mysite.com/something_else.php,
then
include(file_for_something_else.php);


What is the syntax for this kind of includes if that exists? Thanks.

nephilim 09-05-2003 02:25 AM

You could try this:

Code:

<?php

if ($_SERVER['HTTP_REFERER'] == "http://mysite.com/something.php") {
    include(file_for_something.php);
} else if ($_SERVER['HTTP_REFERER'] == "http://mysite.com/something_else.php") {
    include(file_for_something_else.php);
}

?>

The problem is that HTTP_REFERER is only filled if a user actually followed a link to the current page. If the user typed it directly into their browser or were redirected there, HTTP_REFERER will be empty.

linuxfond 09-05-2003 02:53 AM

Thanks a lot for your kind help. I see the idea.
I wanted to use this kind includes to include different meta tags for different pages. It should be readable first of all for search engines.

I was eventually thinking about fopen(). I.e., first check whether the file exists and is opened, and then include the necessary meta tags. There is just one template on the site, and lots of inserts: index.php?page=1, index.php?page=2 etc.

HTTP_REFERER works exactly the way you told, but it can't be used for calling meta tags, because it calls wrong meta tags for wrong pages. I did not realize it before I tested it on my localhost.

linuxfond 09-05-2003 07:37 AM

Now I am trying this:
PHP Code:

<?php
define 
("URL_ROOT""http://127.0.0.1/");
if (
$_SERVER['URL_ROOT']."/index.php?page=main"){
     include(
'meta/meta.inc');
}
if (
$_SERVER['URL_ROOT']."/index.php?page=widgets"){
     include(
'meta/meta_widgets.inc');
}
?>

The script does insert the right meta into the page=main, however, it doesn't replace the meta with meta_widgets in page=widgets.
:confused:

nephilim 09-05-2003 07:56 AM

Hm, seems to me you're testing a String and this will always return true.

Imho both files would have to be included if you do it like that.

Andy@DP 09-05-2003 08:13 AM

nephilim has a good point
Code:

if ($_SERVER['URL_ROOT']."/index.php?page=main")
that if statement has no comparison as in if (a == b) its just a string and as he said it will return true.
It should be
Code:

if ($_SERVER['URL_ROOT']."/index.php?page=main" == something)
or the first if will always return true!
If the page URL is exactly the same until the ?page= GET statement why not test
Code:

if ($page == "main") {statement}
elseif ($page == "widgets") {statement}


nephilim 09-05-2003 08:28 AM

returning the favor: Andy@DP has a good point too ;)

The name of your page is indeed an url parameter, so it would be in the $page variable.

linuxfond 09-05-2003 09:15 AM

Hi Nephilim & Andy@DP,

Thanks for all your comments!
This, of course, works:
PHP Code:

if ($page == "main") {statement
elseif (
$page == "widgets") {statement

It places correct meta tags for each page.

I am happy to have learnt so much in just a couple of days! THANKS A LOT!
:D


All times are GMT -5. The time now is 11:03 AM.