I have a only one .php (root/process.php) file for multiple languages
root/en/command.htm
root/fr/command.htm
root/ru/command.htm
and so one. However, for each of commands I have a thankYou.htm in the same folder:
root/en/thankYou.htm
root/fr/thankYou.htm
root/ru/thankYou.htm
How do I redirect the page after processing it in the process.php?
// redirect to a thank you page
header("Location: " .$_SERVER['HTTP_REFERRER']. "\thankYou.htm");
this does not work: Error 404. Normally, if the referrer is root/ru/command.htm by e.g., so the php should sent user to root/ru/thankYou.htm etc.
Try a slash instead a backslash:
header("Location: " .$_SERVER['HTTP_REFERER']. "/thankYou.htm");
In HTTP it's misspelled as "referer", so you want $_SERVER['HTTP_REFERER'].
Related
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/..'
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.
I have main.php file, on which I have many "sub pages" (there are tabs and you can switch them so http://mydomain.com/main.php address does not change). I receive them dynamically via ajax requests. On one of such "sub page" I have download links. For example
fish
in dwnl.php file I have something like this
// . . . . .
$file =basename($_GET['file']);
if(file_exists($file))
{
// download file
}
else exit;
my problem occurs when file does not exist (i.e. in 'else' block of my dwnl.php file). If users click such link, browser redirects them to http://mydomain.com/dwnl.php?file=... address. How can I forbid such redirection? I would like to do it using PHP (if it is possible). So, what should I do in 'else' block?
In else you can redirect to $_SERVER['HTTP_REFERER'];
This is redirect to your referer page.
Else if possible to check earlier
you can check file_exist and place href if file exists.
One possible solution, depending on the way your script is setup, is to use a header redirect.
<?php
header( "LOCATON: /not-found.php" );
die();
?>
This will redirect the user to whatever page you want. As some other people have already said though, you should be very careful about dealing with file downloads in that way to prevent various exploits. I would sanitize the filename before passing into any functions that grab file contents and also limit the possible download directories to only the locations you absolutely need to download from. Don't trust open_basedir to protect you in any of the above situations, either.
1) You may output a simple html with an error message and with a timeouted meta refresh tag.
Redirect to http://example.com/ after 5 seconds:
<meta http-equiv="refresh" content="5;URL='http://example.com/'">
2) You may use header() to redirect immediatelly without any message:
header("Location: http://site.com/page?var=123");
3) You may set error 404 file not found:
header('HTTP/1.0 404 Not Found', true, 404);
This is a simple question which makes it painfully obvious that I need to take a php class...
I have as the first part of a an if / else statement that reads:
if (is_user_logged_in()){
//echo "user is signed in<P>";
header("Location: user-homepage.php");
so if the user is logged in and clicks a link that directs to /register.php, they should instead be redirected to the user-homepage.php.
What happens is they are directed instead directed to /register.php/user-homepage.php
My code is adding /user-homepage.php to the address instead of replacing /register.php with /user-homepage.php
What have I done wrong?
Use an absolute path rather than a relative one:
header("Location: /user-homepage.php");
Try:
header("Location: http://your_domain.com/user-homepage.php");
The PHP manual says to use Absolute URLs.
You have used a relative file path. Try adding a forward slash to make it relative to the domain root.
header("Location: /user-homepage.php");
As given in section 14.30 of RFC 2616, "HTTP 1.1", use an absolute URL in the Location header.
I am trying to redirect www.xyz.com/my to www.abc.com, how can I do this in PHP?
Strictly speaking you need to send a HTTP location header to the clients browser.
To do this in PHP, as the other answers have mentioned is to use the header() function.
header("Location: http://www.abc.com");
There is a caveat that you should be aware of. The most common problem people encounter when dealing with HTTP headers is that they must be sent to the browser prior to any other data. If you echo any content to the client and then try to send the location header, it wont work.
For reference, there are many other HTTP headers that you should familiarize your self with.
You may do that in PHP by creating a subdirectory called /my on the http-root of the server where www.xyz.com is hosted and then make an index.php file with <?php header("Location: http://www.abc.com"); ?> inside it.
However, this is not the right way to do it. What you should do it create a .htaccess file right in the root folder of www.xyz.com looking like:
redirect /my http://www.abc.com
You can send a header redirect:
header("Location: http://abc.com");
If the default page for a directory is index.php, just put that code under /my/index.php. Any visits to the /my page will redirect.
header("Location: http://www.abc.com");
wrong technology. You probably want to look into apache htaccess, which is generally what most people use to host php.