Jump to content

Linking or Server reference help!!!


chrisg7504

Recommended Posts

Hi,

 

I am a total newbie at PHP and am trying my best to understand some things.

 

I have a file called header.php located in includes/header.php

 

I also have a file called h_shop.php located in cart/h/h_shop.php

 

The problem I am having is how to automatically reference the header include using php instead of a numerous amounts of slashes.  A config file would help as I have a number of pages in the folder cart/h/

 

This is what I started with but doesn't work mainly because I am testing on a local server and when I upload it I have to change all the files references again:

 

<?php

require("../../../include/header.php");

?>

 

Any help or understanding of this will help greatly.

 

Thanks

Link to comment
Share on other sites

I would recommend using constants as base directories and its always better to use absolute paths than relative paths and its faster

 

It's also better to include files from an index page, its much easier to include since u don't have to calculate where are u now in the application

 

so if you did:

index.php

<?php
require('include/header.php');
require('cart/h/h_shop.php');
?>

 

and manage it with get variables

for instance you want the index.php to load the h_shop script

you can call do this:

<?php
require('include/header.php');
if($_GET['go'] == 'shop'){
require('cart/h/h_shop.php');
}
?>

 

and call it index.php?go=shop

 

you will need to use a switch statement if you got many pages

 

hope that helped :)

Link to comment
Share on other sites

Hi,

 

I am a total newbie at PHP and am trying my best to understand some things.

 

I have a file called header.php located in includes/header.php

 

I also have a file called h_shop.php located in cart/h/h_shop.php

 

The problem I am having is how to automatically reference the header include using php instead of a numerous amounts of slashes.  A config file would help as I have a number of pages in the folder cart/h/

 

This is what I started with but doesn't work mainly because I am testing on a local server and when I upload it I have to change all the files references again:

 

<?php

require("../../../include/header.php");

?>

 

Any help or understanding of this will help greatly.

 

Thanks

 

Hi thanks for the quick reply.  Your method is what I used first off.  But my issue is that on my local server the header.php file is located in www/hosting/include/header.php and the remote file location is public_html/include/header.php which kinda of throws me off a little.

 

 

Link to comment
Share on other sites

It can be search engine friendly by using apache mod_rewrite but this is another topic, you'll have to search for some tutorials

 

.. you can do this in the very beginning in your script but I prefer doing it in a separate configuration file

 

<?php

// server base directory
define('BASE_DIR','public_html/');

// local base directory
define('BASE_DIR','www/hosting/');

require(BASE_DIR . 'include/header.php')

?>

 

comment any of the defined statements u don't use

e.g. on the server comment the define statement of the local directory

Link to comment
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.