Hi

I wonder if anyone could help me with this idea.
I would like to automatically redirect a user once they have logged in. An good example would be like daniweb's, a page appears saying 'you will be redirected in 3 seconds otherwise click here'.

Is this done through javascript, php or ajax ?
How would it be written?

Thanks for any help

Dani AI

Generated

A few practical options and gotchas to pick up from the replies: pointed to general resources and showed a server-side refresh approach. For login flows the clearest, most robust pattern is to perform the redirect server-side when possible, and only use delayed client-side redirects when you need to show an interim message.

Server-side (recommended after successful POST)

  • Set session/state, then issue an HTTP redirect so the browser loads the destination with a GET (avoids form re-submission). Use an appropriate status (303 after POST is a good choice).
  • Example PHP flow:
// after validating credentials and starting session
header('Location: /dashboard.php', true, 303);
exit;

(See the PHP header docs: https://www.php.net/manual/en/function.header.php and HTTP 303 guidance: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303)

Client-side delay (show message + fallback link)

  • Render a page that says "You will be redirected in 3 seconds" and include a clickable link. Use JavaScript to redirect after the timeout; prefer window.location.replace() so the login page is not kept in history.
<p>You will be redirected in 3 seconds. If not, <a href="/dashboard.php">click here</a>.</p>

<script>
setTimeout(function(){
  window.location.replace('/dashboard.php');
}, 3000);
</script>

(See MDN on Location.replace: https://developer.mozilla.org/en-US/docs/Web/API/Location/replace)

AJAX logins

  • Return JSON with a redirect URL and let the client JS perform window.location.replace(resp.redirect) on success. That keeps the UI responsive and simple.

Security and practical tips

  • Never trust an arbitrary redirect URL from input; validate or restrict to relative paths to prevent open-redirects (OWASP guidance: ).
  • When using PHP redirects, send them before any output and call exit afterwards.
  • Meta refresh is an option for very simple pages but is less ideal for accessibility/SEO than the approaches above (MDN meta refresh note: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#attr-http-equiv).

These patterns cover immediate server redirects, timed client-side redirects with visible messages, and AJAX-based flows—pick the one that matches your UX and security needs.

Recommended Answers

All 2 Replies

If you do a search, you'll easily find info on this. One link that may be useful is:

header( 'refresh: 5; url=http://www.example.com' );

Can also be a url relative to your site, like: home.php etc.

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.