Jump to content

PHP Scripts layout


Go to solution Solved by ginerjm,

Recommended Posts

Hi Everyone i know this is a bit basic, but I have been working on a project and to tidy things up i have been placing them in directories.

 

my problem is I need to know how to plan this so i dont need to call an "include" or "require" in every page.

 

as it gets difficult when you are going back a directory and then into another directory for example:

 

in the html folder I have

 

index.php

 

sessions/login.php

 

functions/functions.php

 

index.php can all the functions via "include ('functions/functions.php');

 

but if we need the login.php to require functions it needs to be ('../functions/functions.php');

 

this can sometimes having something like a header.php in every page as it would call the script wrong directory.

 

would some be able to help so that i either don't need to include a script on every page, or maybe i can define functions that are a constant?

 

please let me now if this makes no sense! as ts a little difficult to explain.

 

Thanks in advance for any help given <3

Link to comment
https://forums.phpfreaks.com/topic/299460-php-scripts-layout/
Share on other sites

Why not use constants for the path names and include a module that declares all these constants.  Then your include simply reference the correct constant the the simple filename.  You move things around - update the constants.  Make them absolute too!

Link to comment
https://forums.phpfreaks.com/topic/299460-php-scripts-layout/#findComment-1526290
Share on other sites

  • Solution

So you have a little module that defines paths

 

/*  paths.php

$php_path = "/home/domain/php/";

$lib_path = "/home/domain/public_html/libs/";

...

...

 

 

Then in your code you include the above file and in your other includes you use the appropriate path var:

 

include($php_path . "connect.php");

include($lib_path . "functions.php");

...

...

Link to comment
https://forums.phpfreaks.com/topic/299460-php-scripts-layout/#findComment-1526293
Share on other sites

Hi QuickOldCar thanks for the response, I am using this function

function url(){
  return sprintf(
    "%s://%s/",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_ADDR']
	//$_SERVER['SERVER_ADDR']
  );
}

But i have to call the functions php before i can use it lol :D

Link to comment
https://forums.phpfreaks.com/topic/299460-php-scripts-layout/#findComment-1526320
Share on other sites

For server includes is no need for the protocol, you can link to local files.

 

Additionally can link to css,images and such within the html using absolute url without a protocol

<img src="//mysite.com/image.png" />

 

Check out some server variables can use

 

A while ago I babbled on a bit about the differences in a post, take a look.

http://forums.phpfreaks.com/topic/292789-paths-when-rewriting-urls/?p=1498045

Edited by QuickOldCar
Link to comment
https://forums.phpfreaks.com/topic/299460-php-scripts-layout/#findComment-1526322
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.