Php page redirecton - php

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

Related

Got 404 when redirect every url to index

So, I've been trying to do the Wikipedia url thing! Like making the url readable. Tried every .htaccess code out thire and contacted the Hostinger customer services to help, and nothing works! I don't know why.
So, i move to use the PHP, and i got work great on test.php page that i use the test on, but when i move the code to index.php it doesn't.
So, the why i did it is having a page with php simple code: if url[2] has s include s.php, else include x.php. and it works great when i go to www.website.com/test.php/s it will open s.php
However, when after i move the code to index.php and then i go to website.com/s i got an error as this page is not in the srver, but if i go to website.com/index.php/s it works.
So what do think i could fix it with? Or if there is any other why to do it. Thanks in advance ☺

How do I stop user being able to see website code

the dilemma I have is my website index.php calls to a template php file on a button press like this:
case 'main':
$page = getTemplate('main.php', array('user'=>$user));
echo $page;
break;
This main.php template file is in a folder in "/var/www/template/" How do I stop people going to: domain.com/template/main.php and viewing the code for that page. I think the solution would be to make the localhost be able to pull the it and display it rather than the user or something along those lines. Any help would be appreciated thank you.
Like a comment said, the PHP file will not be printed, it will print the HTML result that the php file produce.
Maybe it produces some errors indicating vulnerabilities to a potential attacker ? If that's your case, you should handle this directly into the php code or use a .htaccess at the root of your site. You can't find some help there.
How to deny access to a file in .htaccess
Managed to fix this by putting this at the top of the php page I wanted to render:
<?php
if (!isset($_GET['page'])) {
header('Location: /main');
exit();
}
?>
This means if someone goes "domain.com/template/main.php" to attempt to view the source code, it will redirect them back to the main webpage for my site. Thanks for your suggestions however.

Header Location is not working properly even when everything looks ok in code

I have the following folder structure
/main/site/
the redirect script is in the following dir
/main/site/backend/
header('Location: ../register.php');
returns to /main/register.php/ when it should go to /main/site/register.php
It seems all ok in the code , since ../ should go back one dir,
someone know what is wrong?
It does not matter where your script resides - every Location instruction applies to what the outside looks like. If the script is requested thru https://www.example.com/main/site/backend/filename.php then it must redirect to ../../register.php or even better /main/register.php.
Try changing to this
header('Location: ./../register.php');
./ Forces PHP to look only in current directory
See this post

header('Location:') not working

So, two questions!
When using header('location: newHome.php') Does that file need to be in the same directory?
I was going to have my signOut.php in the main directory, and be able to sign out of the website from any page.. example.com/example/example.example.html
My code just returns a error page at example.com/signOut.php The signOut.php is in the same directory as where I am testing it from..
example.com/example.php
<li><a href="signOut.php"><strong>Sign Out</strong>
example.com/signOut.php
<?php
session_start();
$_SESSION = array();
session_destroy();
header('Location: http://www.example.com/newHome.html');
?>
1) Answer to your 1st Question (When using header('location: newHome.php') Does that file need to be in the same directory?)
If you will not give any path then Yes it must be in the same directory.
Though, you need to define constant for your website URL like below:
define(SITE_URL,"http://www.example.com/");
And then use it anywhere in your website like below:
<li><a href="<?php echo SITE_URL; ?>signOut.php"><strong>Sign Out</strong>
Follow below link to learn more about Constants :
http://php.net/manual/en/function.constant.php
2) Answer to your 2nd Question (My code just returns a error page at example.com/signOut.php)
This is happening because your path is not correct. Manually correct the path or follow my first answer and define constant to correct it.
I don't think there is any issue with Headers as error you are saying with 404 Not found.
either, in the same folder
header('location: newHome.php');
or, not in the same folder
header('location: path/to/newHome.php');
or, a full URL
header('Location: http://www.example.com/newHome.html');
ob_start();
header('Location: http://www.example.com/newHome.html');
just use ob_start(); before header it will help

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.

Categories