I've had a previous question answered on how to add a / to my url and redirecting it internally. The answer I got worked great, see here but i've stumbled on a new problem.
When you recover your password you are forced to change it:
if (logged_in() === true) {
if ($current_file !== 'changepassword.php' && $user_data['password_recover'] == 1) {
header('Location: changepassword.php?force');
exit();
}
}
When you're on the index page, and are forced to change your password, the URL goes from mysite.com//index/ to mysite.com//index/changepassword/?force which obviously does not exist.
When I use ?force without changepassword.php it gets a "this page has an this page has redirect loop" warning, although it did work for other things like ?success.
Base href="/" also doesn't have any effect.
Any ideas?
My guess of your problem is that you replace a "terminating" .php with /. So /index.php turns into /index/. If you now redirect to changepassword.php?force, you will end up with /index/changepassword/?force.
But as the script changepassword.php likely is located at the same level as index.php, you have to use a host relative redirection, instead of a path relative redirection:
header('Location: /changepassword.php?force');
Please note the leading / in the location target.
Related
Someone may saw the similar question like this,but I can't find the complete resolution about this.
now here has a link www.example.com/test/index.php, if I add an slash to the end, like www.example.com/test/index.php/,it would get
redirected you too many times
so I use this code to filter the url:
if ($_SERVER['REQUEST_URI'] != "/" && preg_match('{/$}',$_SERVER['REQUEST_URI'])) {
header ('Location: '.preg_replace('{/$}', '', $_SERVER['REQUEST_URI']));
exit();
}
it would turn index.php/ into index.php
but now if I just go www.example.com/test/ it would get
redirected you too many times.
so now my question is, how can I let them both work?
P.S. My server can't use .htacess
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.
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 have a site I'm working on with a /landing-page/ folder, and I'll be making a number of landing pages. I'd like to be able to put urls that have /lp/ instead of /landing-page/ in advertisements, and have any url with /lp/ in it replaced with /landing-page/.
Thus:
www.site.com/lp/ipad-inspections
would automatically redirect to:
www.site.com/landing-page/ipad-inspections
I would like to do this without having a /lp/ folder and a second page corresponding to each landing page. My thought is to have the 404 page check the url and redirect, but I can't seem to get the following to work:
<?php /* Automatic redirect for landing pages */
$current_loc = $_SERVER['REQUEST_URI'];
$short_lp = '/lp/';
$long_lp = '/landing-page/';
if (strpos($current_loc, $short_lp)) {
$current_loc = str_replace($short_lp, $long_lp, $current_loc, 1);
header("location: ".$current_loc);
}
What am I doing wrong here, that my page is coming up blank? I have narrowed it down to the first line in my if statement, which is crashing my page.
If there a better way to do this with apache? Some line I can put in htaccess, maybe?
RedirectPermanent /lp /landing-page
in your .htaccess should do the trick. Best to do this sort of unconditional redirect BEFORE it reaches the PHP stage. It's essentially a "free" operation in Apache, and saves your server the whole parse/compile/execute PHP stages.
Keep it as you've done it but include the 301 permanently moved header.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Also try referencing your URL absolutely, so as to include http://www.domain.com/
Update
If you want to redirect with htaccess try
redirect 301 /lp/landing-page/ http://www.domain.com/new.html
I want to check which URL someone is currently on. For example:
if(url=index.php?p=contact) {
echo the code i want to run,
}
else {
do nothing
}
So basically, I want to run a block of code when the user is on index.php?p=contact
The current requested URI path plus query is available in $_SERVER['REQUEST_URI'] and the filename of the processing script in $_SERVER['SCRIPT_FILENAME'].
If you need to check the complete path, see Gumbo's answer. If index.php is only accessible by navigating to that name directly (that is, you know if index.php is being executed the user must've gone to index.php, and you're not using something like URL rewriting), it probably makes much more sense to just check:
if($_GET['p'] == 'contact')
within index.php. If the condition is being reached, index.php is executing and clearly that's the page the user is on
This is what I did (to make it as I want with .htaccess); works for me since I do not have allot of pages to check. I used PHP_SELF:
<?php
if (htmlentities($_SERVER["PHP_SELF"]) === "/.../index.php") { // echo $_SERVER["PHP_SELF"] to see your path ..
header("Location: ./"); // I am using .htaccess, so I only want the page name and exclude ".php" in the address-bar (URL)
die();
} else if (htmlentities($_SERVER["PHP_SELF"]) === "/.../page2.php") {
header("Location: page2"); // I am using .htaccess, so I only want the page name and exclude ".php" in the address-bar (URL)
die();
}
?>