how to redirect to a page in a specific folder... how to specify path...

Dani AI

Generated

For akhilckz: the simplest working approach is to send an HTTP Location response from the login script that points to the register page in the folder you mentioned. Several answers here already pointed to that mechanism (thanks to , and ), so below are the practical details people often miss and which will make the redirect reliable and safe.

Decide whether you want a root‑relative path (starts at your site root) or a full absolute URL (includes scheme and host). A root‑relative path is fine for internal redirects; a full URL is required if you redirect to another host or switch from http to https. Remember you must send the redirect before any output is emitted by the script — whitespace, UTF‑8 BOMs or echoes in included files will break it.

Typical post‑login workflow: validate credentials, start/set session state, pick the destination (prefer a fixed path or a validated/whitelisted value from any next parameter), then send the redirect and stop execution so no further output or side effects occur. If you get “headers already sent” errors, check for accidental whitespace, saved BOMs, debugging prints, or output from included files; enabling output buffering is a last‑resort workaround while you fix the root cause.

Security and behavior notes: never redirect directly to an arbitrary user‑supplied URL — use a whitelist to prevent open‑redirects. If you’re redirecting after a POST, use a status that ensures the browser does a GET (to avoid form resubmission). If you need the original HTTP method preserved, choose the appropriate 3xx code that preserves it. Finally, inspect the response in your browser’s Network tab to confirm the Location header and status code are what you expect.

Recommended Answers

All 5 Replies

Can u please explain a bit more.

Ok one folder contain reg.html
I want to redirect to this page from login page how can I specify path for this. Header(location:?????)

header('Location: /folder/foo.html');

Summary of existing answers plus my own two cents:

1. Basic answer

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

header('Location: '.$newURL);

2. Important details
die()
header("Location: http://mydomain.com/myOtherPage.php");
die();

Why you should use die(): The Daily WTF

Absolute URL

The URL must be an absolute. See RFC 2616. But in most cases a relative URL will be accepted too.

Status Codes

PHP's "Location"-header still uses the HTTP 302-redirect code, but this is not the one you should use. You should consider either 301 (permanent redirect) or 303 (other).

Note: W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.

3. Documentation

HTTP Headers and the header() function in PHP

What the PHP manual says (read the link in the very bottom for where i got the answer)
What Wikipedia says (read the link in the very bottom for where i got the answer)
What the W3C says (read the link in the very bottom for where i got the answer)

4. Alternatives

You may use the alternative method of http_redirect($url); which needs the PECL package pecl to be installed.

5. Helper Functions

This function doesn't incorporate the 303 status code:

    function Redirect($url, $permanent = false)
    {
        header('Location: ' . $url, true, $permanent ? 301 : 302);

        exit();
    }
Redirect('http://www.google.com/', false);

This is more flexible:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}
6. Workaround

As mentioned header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:

<meta http-equiv="Location" content="http://example.com/">
Or a Javascript redirect even.

-------------------------------------------
Hopefully I read and understood your question properly. I got the answer from: http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.