PHP: Problems redirecting relative paths with header() - php

I am doing a project with XAMPP from my university in which they ask me to use the header function to perform redirects and a file structure to follow. But I'm having some problems.
I have the following file structure:
backend/validateSession.php
backend/show.php
index.php
login.html
So, validateSession.php has this inside:
session_start();
if(!isset($_SESSION["DNI"])){
$path = "./login.html";
header("location: ".$path);
}
And both index.php and show.php have to include validateSession.php
index.php:
include_once "./backend/validateSession.php";
show.php:
include_once "./validateSession.php";
When entering index.php, validateSession.php correctly redirects me to login.html
But if I enter from show.php, I have an error because the path is ./login.html instead of ../login.html
So my question is: How could I solve this problem if index.php and show.php need different paths so that header can redirect me correctly? Thanks in advance.
EDIT: header should redirect me to localhost/prog/TP/login.html, but
if i use this path in the header, it probably won't work if someone else tries to access it from their own XAMPP.

The path for an HTTP redirect is relative to the domain, not to the filesystem (since the browser knows nothing about that). In this case, you should just be able to use
$path = "/login.html";
header("location: ".$path);
Note the lack of . at the start of path. domain.com/index.php, domain.com/backend/show.php (and any others) will all redirect to domain.com/login.html

Related

Php page redirecton

I have been browsing a good couple hours regarding php page redirection. It should be quite straight forward task. However, I am unable to understand what is going on...
I have tried the following two lines of code.
header('Location: http://www.google.ca');
It works!
header('Location: localFile.php');
It does not work!
//redirect.php
<?php
header('Location: localFile.php');
?>
//localFile.php
<?php
echo "good!";
?>
My public_html directory contains localFile.php and redirect.php. I don't think that my code is wrong!! Hope someone can tell me what is going on...
A location in the headers refers to the URL and not to files. If the file you want to redirect to is in the root, add a forward slash:
header('Location: /localFile.php');
Your first attempt with https://google.ca worked because it refers to an actual URL

PHP Header Location: ../location vs $_SERVER[DOCUMENT_ROOT]/location

So, I made a simple PHP login, but when I tried to redirect like this:
$path = $_SERVER["DOCUMENT_ROOT"];
header("Location: $path/admin/index.php");
it seemed like it did nothing, but after I refreshed the page I was logged in.
After I changed my code to this:
header("Location: ../admin/index.php");
it works.
Could someone please explain this to me?
Ps. sorry for my bad english
The header is sent to the browser, so it is not an internal server maneuver. And with it not being an internal redirect, you don't deal with internal paths. When you use DOCUMENT_ROOT you will get the internal server path to the directory where your files are located.
If you want to reference the root of the site as a URL, just use /.
header("Location: /admin/index.php");
header("Location: /"); # go to homepage, for example
Your .. worked because you probably were on a subdirectory, and .. was translated to the parent directory which is where admin is.
$_SERVER["DOCUMENT_ROOT"];
returns path like /var/www/html/yourfolder/, but you have to redirect to website.com/yourfolder/ or localhost/yourfolder/.
hence that won't work.
Have you tried printing the value of $path?
the value of $path is relative to the actual file location
e.g. $path = '/c/inetpub/sites/example/main/'
You probably wanted something like '/c/inetpub/sites/example/' or '/c/inetpub/sites/example/main/..'

php header function prepending the current directory

I am new to php and I faced this problem while redirecting to other php files. To make my question clear I created two php files. first.php and second.php.Both this files are found in /var/www/test/.
This is the code in first.php
<?php
$url = "localhost/test/second.php";
header("Location:$url"); ?>
This is the code in second.php
<?php
echo("this is the second page"); ?>
And when I browse to first.php file I got this in my Firefox browser.
The requested URL /test/localhost/test/second.php was not found on this server.
test/ is added to the header string, which is clear as you can see. Thank you.
The server is appending your parameter to the current working directory. To make it clear to your server, that this is an entirely new URI, prepend your parameter with http://. That way it becomes an absolute URI:
$url = "http://localhost/test/second.php";
If you have problems with localhost not being resolved properly, refer to the questions linked by #gen.

Fix a redirect loop?

I have the following code in my index.php page:
<?php include("/includes/widgets.php") ?>
And in my widgets.php page:
<?php
header("Location: /");
?>
What I want to achieve with this is to redirect it if the user visits it, but allow it for including.
But I get the following error:
The webpage has a redirect loop
How can I fix/prevent the redirect loop, but still redirect the user and not the server.
Place the widgets.php file in a folder not accessible to HTTP clients. I.e on apache webserver, either put it above your document root (into it's parent) or use .htaccess file to block users from it.
e.g.
deny from all
I think I know what you need :)
Change code index file to next
define("IS_INDEX", true);
include("/includes/widgets.php"
Change code for widgets.php to next
if (!defined("IS_INDEX")) {
header("Location: /");
exit();
}
The issue is you are redirecting back to the same page, which then redirect again, and again, and again.
An easy fix would be to wrap the redirect in an if, and only redirect if they aren't already on the index page; but this is just patching what looks like an architectural problem.
Something like:
if (ltrim($_SERVER['REQUEST_URI'], '/') != 'index.php')
header('Location: index.php');
One way is to check if __FILE__, which is the file loaded, regardless of included or not matches up with the file requested which is in $_SERVER['REQUEST_URI'] (or $_SERVER['PHP_SELF']).
I use this on our development site in a page that is usually included to get the output as debugging.
if(basename($_SERVER['PHP_SELF'])===basename(__FILE__)){
//do some debugging
}
Typically you wouldn't use basename, but this is on a non-public facing development site and the file has a pretty unique name so I'm not worried about the file being included with another file with the same name or anything.
One possible way is to add a parameter to the redirection, e.g.
if (!$_REQUEST['redirect'])
header("Location: /ìndex.php?redirect=1");
That way redirection can happen only once.
Another way is to stop redirection if the user already is on the /. I´d suggest to combine both.

PHP header redirect with nested folders

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.

Categories