Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » Basic PHP question RSS

Basic PHP question

This thread was started by banj and has been viewed 1718 times, and contains 25 replies, with the last reply made by Dorky.
Page 1 of 2: 12 > Next Post Reply
1
View banj's reputation
banj, WDF User Private message  
Posted April 14 '09 at 10:35 PM
      Posts: 32
Hi all,

I have created a php site and have 2 basic questions about PHP. The test location of the site is here:
http://www.tyretraders.com.au/www/index.php

1. I currently have pages which 'pulls' the header and footer:
<?php require_once('header.php'); ?> and <?php require_once('footer.php'); ?>

The header and footer are located in the ROOT folder along with all other pages. However, I would like to have some pages in different folders for the site as its getting quite large.

This is a problem because I cannot seem to get the 'require once' script to point to the specific php script, i.e. -
<?php require_once('../header.php'); ?> or <?php require_once('http://www.tyretraders.com.au/header.php'); ?> and it really messes up the site.


2. In the past when I have created a php site I have had to create two index files (www.careerexpo.com.au):
1. index.html
2. index.php

The user lands on index.html and then it refreshes to index.php. Is there an easier way to have the user land directly on the index.php page?


Thanks in advance for any help on the above questions.




Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted April 15 '09 at 09:04 AM
      Posts: 3,088
#2 first ...

You only need "index.php".
The server defaults to the index pages in this order (possibly, not sure) ...
"index.html"
"index.htm"
"index.php"

You can also use your .htacess file to force the server to process
all .html files as PHP (if you want to keep your .html extensions).


#1 ...

Using relative paths is the way to do it (no http:// ..)
It's best to keep the scripts organized ... possibly putting all of the
scripts in the same place might help. The reference is relative to
the position of the PHP script that is running.

Use Google to see the differences between.
include
require
require_once
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

3
46 points at 100%
Posted April 15 '09 at 08:42 PM
      Posts: 489
Try using include_once
Another way is to use absolute path (no URL path)
Like /home/www/header.php

The only problem is that if you change a host or root structure, you need to re-edit all pages

and for #2 if you don't need the .html just delete it
- Webmaster's Planet . Greek Vortal For Webmasters ...
- MyPortFolio - View My Creations

4
View banj's reputation
banj, WDF User Private message  
Posted April 16 '09 at 02:25 AM
      Posts: 32
Thanks heaps guys! I will try these and see how they go.

Another thing I ran into trouble with...

GOOGLE has commenced indexing the site at the /www/ testlocation!
Is this bad news? If I am going to be shifting this across to its home directory soon? Will it effect my SEO if Google finds the pages deleted or re-directed?

Have you come across this before?

5
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted April 16 '09 at 09:39 AM
      Posts: 3,088
Just move it and let things happen.
You might want to create a redirect for 404 errors.
Your webhost probably has a way for you to control 404 (no page found) errors.

Google is resilient and will adjust to your changes.

Once you have your REAL site, you can use this to add the URL and
also to remove the "old test URL":
http://www.google.com/addurl/?continue=/addurl

At the bottom of that page is a link to removing sites from index.
There are several tools to use and Google's own advice (which is good).
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

6
290 points at 100% Repute
Jason, Formally Perad Home page   Private message  
Posted April 16 '09 at 02:38 PM
      Posts: 855
Create a robots.txt file to stop search engines from listing your website. You do not want visitors while you are developing.

As for your problem.

Check out getcwd(). Its a php function which gets the server path to a file.

http://us.php.net/getcwd

You could then have.

include_once(getcwd().'/path/from/root/to/file.php');

7
1 points at 100%
Posted April 16 '09 at 04:22 PM
      Posts: 31
GOOGLE has commenced indexing the site at the /www/ testlocation!
Is this bad news? If I am going to be shifting this across to its home directory soon? Will it effect my SEO if Google finds the pages deleted or re-directed?
You need to tell Google and the other search bots to not index the pages for your test site. You can do that with your robot.txt or you can do it directly in the header code of the test site. Just remember to remove it when the site is live.
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
When the site does go live you don't want the search engines to point to your test site.
My software never has bugs... It just has random features.
Professional Website Design | Web Site Hosting
The Green Speak Network | Live Green Forum

8
46 points at 100%
Posted April 17 '09 at 10:26 AM
      Posts: 489
Meta name robots is not used nowdays.. better use robots.txt
- Webmaster's Planet . Greek Vortal For Webmasters ...
- MyPortFolio - View My Creations

9
View banj's reputation
banj, WDF User Private message  
Posted April 19 '09 at 07:36 PM
      Posts: 32
Thanks for the advice!

10
View banj's reputation
banj, WDF User Private message  
Posted April 19 '09 at 07:39 PM
      Posts: 32
A little confused... If my site is indexing at http://www.tyretraders.com.au/www/index.php

Would you recommend that I move the entire site over without the www/ extension and delete all pages or just keep it where it is?

11
1,318 points at 100% Repute WDFplus Member
Posted April 19 '09 at 08:45 PM
      Posts: 8,408
move everything to your root. you should never have your website in a subdirectory of your domain.
Steven Moseley
President, Transio
smoseley is online now! Reply

12
1,318 points at 100% Repute WDFplus Member
Posted April 19 '09 at 08:51 PM
      Posts: 8,408
oh, also, you should add this line to your .htaccess file:
RewriteRule ^www/(.*)$ $1 [L,R=301]
That will tell Google to look at your root site for the new pages with a 301 redirect.
Steven Moseley
President, Transio
smoseley is online now! Reply

13
View banj's reputation
banj, WDF User Private message  
Posted April 19 '09 at 09:41 PM
      Posts: 32
Thanks Steve,

I assume that code would apply for the current indexed pages that will be moved, so you have helped me greatly.

Thanks heaps.

14
View banj's reputation
banj, WDF User Private message  
Posted April 29 '09 at 12:06 AM
      Posts: 32
Ok,

thanks for everybodys help here!

The site www.tyretraders.com.au/www/index.php is about to GO LIVE. Is there anything I am missing? Does everything look correct?

In order to take care of the pages already indexed by google whilst remaining SEO friendly... I will be implementing the following into a .htaccess file:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
I will REPLACE domain.com and www.newdomain.com with the actual domain name.

15
50 points at 100%
curare, WDF User Private message  
Posted April 29 '09 at 03:14 PM
      Posts: 48
Easiest way to accomplish this is to use PHP's superglobal $_SERVER variable to ensure you include the file from the document root. You can then use the same code in all instances like so:
require_once($_SERVER['DOCUMENT_ROOT'] .'/header.php'); 

16
View banj's reputation
banj, WDF User Private message  
Posted April 30 '09 at 07:41 PM
      Posts: 32
Hi all,

I cannot seem to get the php script to work when I havent got the php page in the site root.

Take for example:
http://www.tyretraders.com.au/news/april/test.php
or
http://www.tyretraders.com.au/www/ne...ervariable.php - using the suggested server variable script.

I have the require once and server script pointing to the php header and footer in the site root but it doesnt get it?

17
50 points at 100%
curare, WDF User Private message  
Posted April 30 '09 at 08:01 PM
      Posts: 48
just adjust your path accordingly. What I posted was pretty generic. In your case you may have to keep going down the path to the destination file. (and this is my fault for not being clearer in my previous post).

require_once($_SERVER['DOCUMENT_ROOT'] .'/path/to/header.php'); 

You may also need to change your call from 'require' to 'include' for example
include_once($_SERVER['DOCUMENT_ROOT'] .'/path/to/header.php'); 

hard to say without seeing the relevant sections of your source code (remember your links to the site produce errors and we can't see the code).

I do this with my site(s). I break the site up into smaller files and use includes to include the relevant php, html, or text file for that section of the content (header, footer, navigation menu). That way, if I change a main link, I only really need to change it in one spot and it shows up on all 50 pages of the site

I hope this points you in the right direction.

PS: Control search engine indexing of your site through robots.txt and not through meta tags or any such. That's what robots.txt is there for.

18
View banj's reputation
banj, WDF User Private message  
Posted April 30 '09 at 08:21 PM
      Posts: 32
Hi,

Thanks for your post. Still having trouble with this

I just cannot get the php include or require once scipts to work if the php doc is not nestled within the root.

For example, I have the header.php in root.

My news items will stay in:

root > news > april test.php

therefore my php require should be: require_once('../../footer.php') or include_once($_SERVER['DOCUMENT_ROOT'] .'/../header.php'); ?>

I just dont know why it wont pick up the header and footer from the root?

19
50 points at 100%
curare, WDF User Private message  
Posted April 30 '09 at 08:28 PM
      Posts: 48
make it more URL like, so 'DOCUMENT_ROOT' is the server name, then the directory path to the file you want. So in your case (I think) it would be
include_once($_SERVER['DOCUMENT_ROOT'] .'/header.php'); 

or something like that.

Something to try is to access that file directly from your web browser and see if you can see it. Also, ensure that superglobals are allowed on your system (should be able to tell with a phpinfo(); command).
Last edited April 30 '09 at 08:33 PM by curare. Reply

20
View banj's reputation
banj, WDF User Private message  
Posted April 30 '09 at 08:47 PM
      Posts: 32
Thanks for your help, although I am still having trouble.

I can access the file: http://www.tyretraders.com.au/www/header.php

But no matter if I try a rel path or dedicated hyperlink, it still wont pick up the php call.

21
View banj's reputation
banj, WDF User Private message  
Posted April 30 '09 at 10:13 PM
      Posts: 32
Sorry, my bad this is now working...

http://www.tyretraders.com.au/www/news/april/test.php

BUT, the PHP header script wont bring up the style sheet...

I dont understand how this could happen? Shouldnt the header php script reference the css sheet as it does with all pages within the root folder?
Last edited April 30 '09 at 10:37 PM by banj. Reply

22
345 points at 100% Donor Moderator Repute WDFplus Member
Wired, Admin and WDF Alien Overlord Home page   Private message  
Posted May 1 '09 at 03:16 AM
      Posts: 6,273
Simple: You are looking for CSS at:
http://www.tyretraders.com.au/www/news/april/css/

but they're located at:
http://www.tyretraders.com.au/www/css/
Admin at houseofhelp.com
WDF Resources: The Rules
Founder/Creator/Admin of ZE SECRET PROJECT!

Was another WDF member's post helpful? Click the positive rating button () above the post.

23
View banj's reputation
banj, WDF User Private message  
Posted May 1 '09 at 09:57 AM
      Posts: 32
Hi Wired,

Thanks for the post. But I am calling my php from 2 directories away, the original PHP file points to www/css/

I cant see any other way this can be done (other than having multiple css files, yuck!)

24
50 points at 100%
curare, WDF User Private message  
Posted May 1 '09 at 03:30 PM
      Posts: 48
Make sure the file that includes the CSS uses the right path too. So if the css is included in header.php, you have to in that file specify the proper path to style.css (or whatever you call your stylesheet(s) ).

so in header.php, you would have this:

<link href="/css/style.css" rel="stylesheet" type="text/css" />

or you could even do this:

<link href="<? include($_SERVER['DOCUMENT_ROOT']); ?>/css/style.css" rel="stylesheet" type="text/css" />

REMEMBER: the path is relative to header.php and not your primary page in this case.

Me personally, my primary page includes the necessary stylesheets and I include the sections that can be done like header graphics, navigation menus, footer links, etc. as I find it's easier that way. Then you're sure you always have the correct path information.

25
32 points at 100%
Posted June 11 '09 at 01:46 AM
      Posts: 76
Hi all,

I have created a php site and have 2 basic questions about PHP. The test location of the site is here:
http://www.tyretraders.com.au/www/index.php

1. I currently have pages which 'pulls' the header and footer:
<?php require_once('header.php'); ?> and <?php require_once('footer.php'); ?>

The header and footer are located in the ROOT folder along with all other pages. However, I would like to have some pages in different folders for the site as its getting quite large.

This is a problem because I cannot seem to get the 'require once' script to point to the specific php script, i.e. -
<?php require_once('../header.php'); ?> or <?php require_once('http://www.tyretraders.com.au/header.php'); ?> and it really messes up the site.


2. In the past when I have created a php site I have had to create two index files (www.careerexpo.com.au):
1. index.html
2. index.php

The user lands on index.html and then it refreshes to index.php. Is there an easier way to have the user land directly on the index.php page?


Thanks in advance for any help on the above questions.




Not sure if this was already covered but you want to make sure that anything you require or include into a php file is indeed a php file. It doesn't have to be, its just a lot safer.

Make sure when you use the require_once function that you use local paths, not URL paths... You have to use curl() to get remote URLs like that.

Secondly, unless there is code in the header and footer that absolutely MUST be on the site, do include_once instead of require_once. There is one difference between them; if require_once('somefile.php'); is used and somefile.php is missing, has an error, or whatever else that somefile.php does to upset php, the entire script halts and will not function. Require is a security protocol more than functionallity to be honest as include will just run a warning at the top of your page but keep processing the rest of the php file.

Hope that helps ya.

I will reinterate... require and include functions are for local paths... not URLs... try using include_once('/home/yourname/httpdocs/yourheader.php'); instead and let me know what happens =)
Lead Web Designer
MLW & Associates, LLP.
W3C Standards Complaint and SEO Friendly
Admin of Vraul

Page 1 of 2: 12 > Next Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 18 Today 09:52 AM
Basic Question Curtster PHP 5 February 11 '08 08:12 PM
Question on basic layout Snuggles Web Design Discussion 2 July 29 '07 04:41 PM
PHP - QUESTION. 3DeSiGnZ PHP 3 July 5 '03 01:02 PM
Basic PHP Uploads Tutorial thexchord Coding Articles & Tutorials 2 May 2 '02 09:28 PM