Jump to content

Recommended Posts

whats the best way to include code from another file into the php file that will be seen on the site,

 

and what effect does this have on the speed? does it hold it up or delay it at all?

 

e.g. making a login script and if the login is succesfull then include('loginSuccess.php') else not succesfull so  include('loginFail.php');

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/
Share on other sites

Using 'REQUIRE' will ensure that the file is included as the parser looks for 'REQUIRE's prior to parsing the file. Thus 'REQUIRE' files are always included. However the 'INCLUDE' may be avoided via programming.

 

i.e.

$status = 0;

if $status = 0 {
include_once '.\temp.php';
}
else {
include_once '.\temp1.php';
}

In this case the file to be included will depend on the status of the variable '$status'.

 

In the other case,

if $status = 0 {
require_once '.\temp.php'
}
else {
require_once '.\temp1.php'
}

then BOTH files(temp and temp1.php) would be included, irrespective of the value of $status.

ok thanks, so what about the time taken to include these files, will it effect the speed at all, especially if you use a template page where you include a top part and a bottom part, so a minimum of 2 includes per page.

 

Is there any better way to do this (make a template) that you reccomend?

 

thanks

ok one more question, if I am including 2 files, and I want to start a session, i.e. for a restricted area, do I need 2 sets of this:

 

<?php
session_start();
   	if(!isset($_SESSION['id'])){
	header("Location: pleaseLogIn.html");
	exit;
} else {
?> 

          ......html <?php } ?>

 

logically i would have thought that I need only include it once, but it seems that the server will only let it go if I put the piece of code above in both include files, so effectively i'm starting the session twice in the same php web page, that cant be right?? :-\

so for the bottom include, then technically there is no way of protecting the direct url as the code will not be protected by

 

<?php
session_start();
   	if(!isset($_SESSION['id'])){
	header("Location: pleaseLogIn.html");
	exit;
} else {

 

unless I change the read/write permissions of the file( i think?!)

You can stop the file being accessed directly by placing the following in it....

 

<?php

  if (__FILE__ == $_SERVER['SCRIPT_NAME']) {
    die("You are not permitted to access this file directly");
  }

?>

 

If you have a script that is trying to access a script/file that is on another server, will this block that access?

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.