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.
Related
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";
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');
I have a nested folder structure and I want to do a header redirect to login.php from every page that is not authorized. (Every page will include security.php)
NOTE: This whole folder could be in a subfolder so doing DOMAIN + login.php won't work.
Example:
-index.php
-login.php
-security.php
-people.php
-activities
- view.php <-- How can I have do a header redirect back to login.php from here
security.php
if (!isAllowed())
{
header("Location: login.php");
}
index.php
require_once('security.php') // Works fine
activities/view.php
require_once ('../security.php'); //Works fine, but the HEADER redirect doesn't work
activities/view.php ->
require_once ('../security.php');
the ../ indicates that you move a folder back.
There's a lot of things you can do. I'd recommend setting a constant to define the current path and then use that to create an absolute link. You could also use server header data to figure out your current path and decide what the destination is based on that. Also, in php5+ use
require_once 'security.php';
Instead of ()
In my experience, by far the best approach is to define a useful include_path for the application.
In php.ini, we'd set our path like so:
include_path = ".:/path/to/webroot/your_app/includes"
...and then, in our application, include any contents of that directory with a simple include 'file.php';. This abstracts all that ugly path stuff out of our application and leaves the logic clean. We can also make it easy to reuse code across applications:
include_path = ".:/path/to/webroot/your_app/includes:/path/to/shared/includes"
If you do not have access to your php.ini, you can use PHP's set_include_path function. This approach also improves portability.
Updated this post according to the asker's update:
header('Location: /login.php'); should work.
For a small comments widget.
I'm trying to include that widget into any .php file at wish- in the most practical way for the user with just:
<?php include "comments.php"; ?>
I'm trying to create an admin-login-panel right into the widget, to offer the logged admin to delete the posts (and more other options).
The problem is: i have now to start to use the $_SESSION, to prevent the admin having to login over and over again at each change / page-refresh.
But, using sessions inside the widget I can only see header and session WARNINGS ...olready started... ....and so on.
Is NOT an option to force the user to put into his pages top 'session_starts', I'd like to keep things simple. Just php-include the widget.
Can I still keep trying with php sessions or should I try something else?
Thanks in advance for any suggestion.
Alright, with what information you have given us, this is what your problem is: PHP Sessions uses a special cookie that is sent in a header. So you must perform a session_start(); before any other content it sent to the browser, so it can set the cookie in the header. So unfortunately, it looks like you will have to start a session outside of their including of a widget.
Why is starting the session outside of the widget not an option? Do you not have control over the other PHP pages?
It might be possible to configure your site to auto-start the session on each page.
You can typically accomplish this by modifying a PHP configuration value for the directory via a .htaccess file like so:
<IfModule mod_php5.c>
php_flag session.auto_start on
</IfModule>
Also I would recommend referrencing the root path of your website in the include, so you can use that comment include statement on any page without having to add a bunch of '../../../' to the path.
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/header.php");
?>