PHP - Include, Require, Header
I am very confused in between these three terms INCLUDE, REQUIRE, HEADER...
Can anyone tell me the difference between these three terms... I just want to run a php file in the if condition.. Basically i do not want to open i just want to run ... for ex. Code: [Select] <?php if(condition) { // run mail.php file } ?> Similar TutorialsHello. Include is identical to require, only require stops any further script execution. Just wondering if anyone could give me a good example, where 'require' should be used instead 'include'. Thanks. Why would I want to use include() vs. require() ?? (It seems like most people use "include()", but I would think "require()" would be better/safer...) TomTees is there a way to do an include or require to a whole directory? I have only done includes and requires with single files in the past, but have a situation where this could save me a lot of time. Hello, I have a small and annoying problem. Imagine that I have a main folder on my FTP server which has index.php and I then have a php folder for all of my small scripts and an includes folder for misc files. So, for instance http://www.somedomain.com/index.php http://www.somedomain.com/php/somescript.php http://www.somedomain.com/includes/somefile.html Inside somescript.php, I need to use one of the files from the includes folder, like this for example: require('../includes/somefile.html'); This is all fine, but the problem is when I include the PHP file on my index.php page. Then I am telling PHP to go one step back to find the includes folder, but the problem is that it only needs to go one step back if it PHP script is not included on some page outside of the php folder. If I run php/somescript.php, there is no problem, but if I include php/somescript.php on my index.php, then there is trouble! I hope you guys understand what I mean. Thanks in advance! PS - I would appreciate if the solution is not about moving php/somescript.php or using full URLs in my includes. Php Folks, As you know, typing the same code over and over again on all files is daunting. I was wondering, if I can have an error_reporting.php file and then put:
include('error_reporting.php');
at the top of all my php files as header, where the error_reporting.php would have this content: <?php error_reporting(E_ALL); ini_set('error_reporting','E_ALL'); ini_set('display_errors','1'); ini_set('display_startup_errors','1'); ?> Q1. Is that ok or not ?
Q2. Usually, I have a conn.php with content like this: <?php $conn = mysqli_connect("localhost","root","","db_database"); $db_server = 'localhost'; $db_user = 'root'; $db_password = ''; $db_database = 'test'; $conn->set_charset('utf8mb4');//Always use Charset. if (!$conn) { //Error Message to show user in technical/development mode to see errors. die("Database Error : " . mysqli_error($conn)); //Error Message to show User in Layman's mode to see errors. die("Database error."); exit(); } ?>
And then, on all my php files, I just reference to the conn.php by putting the following line on the header: include('conn.php');
Or:
require('conn.php');
And on each php file, just before dealing with mysql, I have a line like this: mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT); $conn->set_charset("utf8mb4"); Now, I am wondering, why should I write the above 2 lines on all my php files that deal with mysql ? To keep things short, why don;t I just add those 2 lines in the error_reporting.php ? So, it looks like this:
error_reporting.php
<?php ini_set('error_reporting','E_ALL');//error_reporting(E_ALL); ini_set('display_errors','1'); ini_set('display_startup_errors','1'); mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT); $conn->set_charset("utf8mb4"); ?>
I am using php require to input a page of code. I am including (require) that file several times within the page. I am guessing that it executes the file every time, and does not save any page loading time, and in fact probably slowing it down because it has to make the request every time.
Just wanted an expert to confirm or deny my educated guess. Would it be the same if it were including an html file instead of a php file?
<?php require ("$base_path/main_page_rotation/content4.php"); ?> Hello all, i just joined today, because am going nuts with php and the include or require statement/function I want to use them to link to a header. Nothing hard about that , I would say. But my bit of code below just does not work. I am running them in a test area on my server. Just to be safe The file L2.php is in the same directory/folder ( showing my age).
I should say that am have been programming a while but only using php for about 4 weeks and this normal simple bit of code has me stumped. I am building the site in visual studio and atom. any pointer would be received with joy
The content of L" is also below . Danler
<?php include_once('L2.php'); ?> centents of l2.php below <h3> line 2 <br><H2>Hello world AM TRYING 6</h2> </h3>
I need to include a pagination file in multiple pages or create a custom function but not sure which one to use. For example: Code: [Select] <?php function test() { echo "Hello PHP Freaks!"; } ?> Or I can create a separate file that echos this statement and include it. When do you use include/require and when do you create your own custom function? Hello, Is this a sound explanation of require and include.. Lets say i have "X" application with an index.php file. in a folder on my domain like so: www.xxx.com/Application X folder/index.php. // the index.php calls to multiple files with-in the "Application X folder" and database. What if i move the index.php to another location all together like so: http://www.xxx.com/notXsubfolder1/notXsubfolder2/notXsubfolder3/index.php //the X application index file.. and then edit the require and include links in the index.php file to paths like this (just example might not be accurate): require_once '../../../../application X Folder/include/required_file.php'; My question:?? What i want to know is: if the required_file.php has includes of its own, will it take the includes that it requires from its original folder where the Required_file.php is located? or will its paths change the the new index.php location?? . Hi, I want to have a require() and a header() on the same page. This is just impossible right? Hello, I have a simple login script that basically, when if you go to a page and you are NOT logged in (saved in cookie) then you get redirected to the login page, else the content is shown. It works, but I currently have my database connection details on the same page, I like to have them on a separate PHP and use require 'connet.php'. So I've gone ahead and done that, but now I get an error saying "Warning: Cannot modify header information - headers already sent". This only happens if I'm NOT logged in. It has something to do with my require "connect.php"; because if I comment that out, it works fine. Here is the script: Code: [Select] <?php //require "connect.php"; // < causes header error // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); //checks cookies to make sure they are logged in if(isset($_COOKIE['ID_my_site'])) { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )){ //if the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']){ header("Location: index.php"); } //otherwise they are shown the admin area else{ ?> - content - <a href=logout.php>Logout</a> <? } } } //if the cookie does not exist, they are taken to the login screen else{ header("Location: index.php"); } ?> My connect.php page is just: Code: [Select] <? // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); ?> Why is the require causing this error, and how do I fix it? Thanks trying to use a require but not working properly Fatal error: require() [function.require]: Failed opening required 'get_extra.php?id=1' (include_path='.') first thing set on page is.. page is profile.php?id=1 $userid = $_GET['id']; code is:require "get_extra.php?id=$userid"; what can i do to fix this? thanks I have a header as a include at the top of each page. When a member is NOT logged in, it displays a header with login option and some non member links, when member IS logged in it displays a header with member options. That part of the script works fine, however when i log in on that page and the member header is displayed.. If i visit another page or refresh that page it loses the session like its been logged out and just displays the standard header. Any help would be greatly appreciated. index.php Code: [Select] <?php session_start(); $path = (isset($_SESSION['LOGINID']) ) ? 'memberheader.php' : 'header_tmp.php'; include( $path ); ?> this is the auth script included in the member header memberheader.php Code: [Select] <?php require_once('scripts/auth.php'); ?> auth.php (the actual auth script) Code: [Select] <?php //Start session session_start();{ } if(!isset($_SESSION['LOGINID'])) { header("location: access-denied.php"); exit(); } ?> I didn't include login page because the login script works fine on each page except the ones with the multi header include. The pages that only include the memberheader works fine I am come across a very odd situation when I include a connnet.php file. For some reason it messes up the layout of the page. However when I enter the connect code onto the page it looks fine. Code: [Select] include("connect.php"); What is also strange is that when I include the header and footer it works fine. For some reason it is just when I include the connect.php file. Is this because it is above the CSS file? Code: [Select] include("header.php"); Code: [Select] include("footer.php"); Is there a better way to define the page name other than require("./anyfolder/any.php"); trouble is i define the page by using ./myfole/anyfile.php but if the page is called from deeper inside my site it dose not work. i have to change all the includes and put an additional . so it ends up looking like this ../myfole/anyfile.php Hey all, I've been having an issue with require_once from another sub-directory. Origionally I had all my php files in the same spot but I wanted to make a library to clean things up a bit. Atm I am trying to access my /subFolder/frame_work/sql/dbconnect.rem.php with require once from /subFolder/frame_work/account_data/accountHandler.rem.php. I have looked around a little bit and i know requires\includes looks in relative directorys. So one obvious solution would be to include the entire path name. "var/www/subFolder/frame_work/sql/dbconnect.rem.php". My problem is I will be accessing this from multiple directories and on localhost the root directory is var opposed to my server which is public_html. I like to edit my stuff in localhost before going public with it and it is going to be a real pain if I have to keep switching var to public_html in every file before I upload. Is there any other solutions I could approach? Any responses would be much appreciated. Thank you for your time. Hi, i have a DB login system and i was wondering if there was a code that i could put on all of my web-pages so that only members (people who actually logged in) could view tem and the rest would be re-directed to /login/loginfailed.php If this is not possible, could there be a way to only allow people who came from a specific url to see the page to see them and the rest be re-directed to /login/loginfailed.php Thanks in advance! I've just downloaded a 3d party app and I'm finding that most (98%) of the [files].php have a "<?php" but not a "?>" in them. I created a tst.php...HW and it seems to not care. An improvement? Mox-Nix? Poor form? Or the same coder the loves 0xEEE text on a 0xFFF background because it's sooooo cool.
Head @ 42 deg /////
Thanks
cal
Okay I have 3 files in the following format: home/run.php home/config.php game/check.php In run.php, I have config.php required as 'home/config.php' and then in config.php I define an absolute path. However, I'm trying to require run.php from check.php but I keep getting an issue because then config.php isn't included. I've tried require using 'home/run.php', '../home/run.php' and even an absolute path. What do I need to do? This is a two part question... my previous post was surrounding an issue with Sessions, now I was told that it may be a server side issue so I contacted my host provider who said to me that sessions cookies REQUIRE https?? now this can't be valid because I've had no issues with sessions prior to this week - second part of this is, is there something I'm missing with maybe an updated versions?
PS: yes, i'm new and simply trying to learn... |