LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2003, 01:27 AM   #1
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
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.
 
Old 09-05-2003, 02:25 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-05-2003, 02:53 AM   #3
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.

Last edited by linuxfond; 09-05-2003 at 03:08 AM.
 
Old 09-05-2003, 07:37 AM   #4
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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.
 
Old 09-05-2003, 07:56 AM   #5
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-05-2003, 08:13 AM   #6
Andy@DP
Member
 
Registered: Aug 2003
Posts: 208

Rep: Reputation: 30
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}

Last edited by Andy@DP; 09-05-2003 at 08:19 AM.
 
Old 09-05-2003, 08:28 AM   #7
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
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.
 
Old 09-05-2003, 09:15 AM   #8
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Original Poster
Rep: Reputation: 30
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use php open and url antony_csf Programming 7 09-27-2004 12:54 AM
php include()/require() script question atom Programming 11 08-19-2004 01:12 AM
Getting the url with PHP linux_pioneer Linux - General 2 06-20-2004 02:08 PM
Can you verify a url with php? digitalgravy Programming 4 03-01-2004 01:38 PM
PHP Url? HappyDude Programming 2 09-20-2003 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration