6

How do I specify a file redirect to a file one level above the current file in PHP? Here is my schematic so far:

-landing.html
-ajax
  -checkLogin.php 

checklogin.phphas the following code in it:

header("location:dashboard.html");

This obviously doesn't work since landing.php is one level above. How can I select a file one directory above? I already tried ..landing.php, but seems like it will only take filenames.

2
  • 1
    For starters, you're missing a slash, ie ../landing.html Commented Sep 18, 2013 at 1:32
  • You mean ../landing.php? Commented Sep 18, 2013 at 1:33

4 Answers 4

9

You should really use absolute paths (at least relative to your document root).

Consider if you move checkLogin.php a directory deeper…

Any of the follow won't have a problem.

header("Location: /landing.html");
// or
header("Location: http://www.example.com/landing.html");
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks! I'll accept the answer as soon as it will let me.
6

The accepted answer will take you to the root of the domain, not 1 folder up as specified.

To go one folder up:

header("Location: ../landing.html");

Comments

3

I think it is because you forgot the / after the ..

header("Location: ../landing.php");

Update: As noted in comments, this is not up to specification, and should be an absolute URI. Another method if you can't get the absolute path for some reason is to use:

$parent = dirname($_SERVER['REQUEST_URI']);
header("Location: $parent/landing.php");

9 Comments

w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 "The field value consists of a single absolute URI."
s/landing\.php/landing\.html
@zerkms A pedantic programmer would certainly use an absolute URI whereas the pragmatic know that modern user agents can interpret a relative URL just fine
@zerkms this is a "strict" format. Browsers will still follow it as expected.
@doitlikejustin: this is a specification. Do you expect browsers to follow HTML specification? And what about HTTP?
|
0

This is the solution which helped me redirect back to the Root(Home) directory.

<?php

    header('Refresh:0 , url=/ROOT_DIR/');

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.