So I was messing around with the twitter api, and I want to include this file in the footer for every page. The footer is loaded via phps require_once function. The problem is I can't use a full url because of url file acsess being turned off for obvious reasons. So I tried to use
require_once $_SERVER['DOCUMENT_ROOT']('/lib/twitter/base.php');
But that failed. What am I doing wrong or how can I do this better?
Try this:
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/twitter/base.php');
Related
I have a website which requires a certain page on all the other pages that I have. My problem is that I can't require this page using require since the link url will change, take this senario as an example:
header.php:
<?php
require('include/database.php');
?>
And then I have a file located at folder: woods/tree.php
If I want to connect to the database from my index file, there is no problem, but if I want to connect to it from tree.php, then the url will not be the correct on header.php anymore. And that leaves me a problem. I tried fixing this by using the entire url:
<?php
require('http//www.example.com/include/database.php');
?>
My problem is now that I can't do that because of security issues with allow_url_fopen and allow_url_include. I will need to find another way to link to that page since I don't want to enable any of the php.ini settings because of its insecurity.
Is there another way for me to require the database so that all files in all folders can access it? Thanks in advance.
Try using an absolute path like:
require $_SERVER['DOCUMENT_ROOT'] . '/include/database.php';
You are seeing a problem where there is none. If I understood you correctly you have this example layout.
|
|-header.php
|-include/
| `-database.php
`woods/
`-tree.php
header.php:
include("include/database.php");
tree.php:
include("../header.php");
This will work, since the include always uses the path from the file where it is included. So tree.php will include ../header.php which includes include/database.php. This will all work nicely.
I don't use php very often and was wondering if someone could answer this question for me.
I have a folder structure like so:
-pages/rightCol.php
-pages/privacyPolicy.php
index.php
In my index file I have a connection to the database like this:
ob_start();
require($_SERVER['DOCUMENT_ROOT'] . "/inc/db.inc.php");
That works fine.
I wanted to separate out some repeated code between pages so I created the rightCol.php file. It needs the connection to the database. So right now I create a query result at the top of the index file and use the statement:
This works.
I also wanted to include it in the privacyPolicy.php page. This does not work because I do not want to put the query code at the top of every page that requires the rightCol.php file.
I would like to put the db stuff inside the rightCol.php. When I try this, then my privacyPolicy.php file works but then my index breaks. Probably because I require the db file twice, once at the top of the index and once in the rightCol.php file.
How can I set this up properly where I do not need to repeat code.
Thanks
EDIT
I changed my call to use require_once.
The privacyPolicy.php page works fine but when I view my index.php it has errors.
Error: No DB selected.
Include the db/inc.php only at the start of your index.php and open the connection. That way, it will stay open throughout the whole script. Then just close it at the very end of your site;
If you are having problem knowing where to include and not ( and still for some unknown reason want to include it more then once ), then get used to require_once method. This way the file will be included only once and the 2nd attempt will be ignored.
Well, the quick way to solve the problem is to use require_once. But i highly recommend that you use a micro-framework like Slim.
A search form in "search.php" is on the vendor's server and it has been set up so my site can display it. The URL is: http://mysite.com/search.php.
The search result page is on the vendor's server also in a separate file called "result.php". It has also been configured so the URL looks like this: http://mysite.com/result.php.
I would like to insert the "search.php" form in the "result.php" file so users don't have to swtich back and forth between the pages.
I tried:
<?php
include 'http://mysite.com/search.php';
?>
It didn't work, and then I realized that I need to set "allow_url_fopen" to yes to use URL in the include statement. This is NOT something I want to do as I read other posts here there is security issue involved.
I'm not sure you can include another server's php file, allow_url_fopen and allow_url_include are to get the file output of the designated url, i mean, the final result, you won't be able to use the variables or functions from that script.
try using file_get_contents(), or if you are displaying final results anyway, you could just use an iFrame
You can just do
<?php
include $_SERVER['DOCUMENT_ROOT']"./search.php";
?>
You can try
include __dir__ . "/search.php";
I'm trying to include db.php and pass a variable to it.
This is what I have:
include "https://imprss.herokuapp.com/db.php?id=6626068";
Looks perfect to me but it simply won't work.
To use a protocol such as https in your include directive, you should make sure that allow_url_fopen is enabled in your php.ini file.
https://imprss.herokuapp.com/db.php?id=6626068 is not returning PHP code, it outputs HTML code to be interpreted by the browser, if you are interested in getting the HTML code of that page then dont use include() instead you should use cURL functions or the fopen url handlers .
I would use a relative path, but only thing you are missing are braces around the quotes which define url.
It is not possible to include a php file on another server, because this would be a security vulnerability. You can, however, use file_get_contents to get the output of a php file.
It's asked before and answered. just go to link bottom:
How to pass parameters with include?
Im loading the php rss parsing library simplepie onto my site and i include it in the header.php file like so:
<?php
// Make sure that SimplePie is loaded
require_once('inc/simplepie.inc');
$feed = new SimplePie('rss.com'); <-- This is a example url
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
?>
Then i try to access the object from another page called page.php and it gives me this error:
Fatal error: Call to a member function get_items() on a non-object in /home/callofdu/public_html/wp-content/themes/Starkers/page.php on line 13
I get the error from this line of code:
<?php
foreach ($feed->get_items() as $item)
{}
?>
Its weird because if i include both of those chuncks of code together on the same php page it all works fine.
I just am not understanding something, please help.
Several things could be wrong here:
Code order is incorrect.
Realise that when you require or include a PHP file, the entire code of that file will be executed at that point. In other words: interpret the command require_once as "Insert file contents here", and check whether the order of lines of code are still right.
You are calling page.php in a separate request.
If you use your browser to manually surf to page.php after the completeion of the previous page, $feed will no longer exist. If you do want it to live on between requests, do the following:
a. Replace all instances of $feed with $_SESSION['feed']
b. In the first line of every file that you surf to (so not the require'd files), put session_start();
The require and include calls in php basically take the contents of that file and slap in place.
So if you follow the linear path that php taking while running a script, you have to make sure that the library is called before you use it.
Basically in practice, if you are executing page.php as a standalone then you will need to require it in that script also, however, if page.php is being included in the first script you mentioned then you don't need to require it again, but make sure the library is included before page.php
ie
<?php
// Make sure that SimplePie is loaded
require_once('inc/simplepie.inc');
$feed = new SimplePie('rss.com');
// Make sure the content is being served out to the browser properly.
$feed->handle_content_type();
require_once "page.php";
?>
doesn't need to be required in both
As for the differences between the require and include calls check out
http://www.php.net/manual/en/language.control-structures.php
EDIT:
After seeing this is Wordpress related, remember variable scoop. Variables do not inherent from a functions parent. So in page.php try:
<?php
global $feed;
foreach ($feed->get_items() as $item)
{}
?>
You need to include the libraries on EVERY page you're using the library's functions.
I think it is relative paths issue. Try using absolute paths. Or check if path to rss.com is okay. May be you need to call it in another way: $feed = new SimplePie('../rss.com');