jas4 Posted August 8, 2007 Share Posted August 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/ Share on other sites More sharing options...
NArc0t1c Posted August 8, 2007 Share Posted August 8, 2007 include("page.php"); to include the page in your script. require("page.php"); for something like variables. Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318320 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318324 Share on other sites More sharing options...
NArc0t1c Posted August 8, 2007 Share Posted August 8, 2007 That's why people learn to use ==. Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318326 Share on other sites More sharing options...
jas4 Posted August 8, 2007 Author Share Posted August 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318335 Share on other sites More sharing options...
pranav_kavi Posted August 8, 2007 Share Posted August 8, 2007 A template wud ve a header & footer that wud be needed across d entire application.In such cases include/require can b used. Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318337 Share on other sites More sharing options...
jas4 Posted August 8, 2007 Author Share Posted August 8, 2007 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?? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318450 Share on other sites More sharing options...
trq Posted August 8, 2007 Share Posted August 8, 2007 logically i would have thought that I need only include it once And you would be correct. Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318454 Share on other sites More sharing options...
jas4 Posted August 8, 2007 Author Share Posted August 8, 2007 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?!) Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318475 Share on other sites More sharing options...
trq Posted August 8, 2007 Share Posted August 8, 2007 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318484 Share on other sites More sharing options...
sKunKbad Posted August 8, 2007 Share Posted August 8, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/63863-php-includesrequire-once-etc/#findComment-318509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.