How to Redirect to Another Page Using PHP

How to redirect to another page using PHP

That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself). So how do I redirect the user from one page to the next?

This means your application design is pretty broken. You shouldn't be doing output while your business logic is running. Go an use a template engine (like Smarty) or quickfix it by using output buffering).

Another option (not a good one though!) would be outputting JavaScript to redirect:

<script type="text/javascript">location.href = 'newurl';</script>

Redirect to another page using PHP

You should user header Function:

header('location: file.php');

in your case:

header('location: /views/login.php');

visit Header Function

How can I redirect another page in php?

This will work if you haven't written anything on the page yet:

header( 'Location: http://www.yoursite.com/new_page.html' ) ;

If you have, you'll have to use javascript for redirection:

<script>window.location.href = "http://www.yoursite.com/new_page.html";</script>

How to redirect user to another page using javascript in php?

Standard "vanilla" JavaScript way to redirect a page

You can use the code below to redirect the user after form submission code.

if( $mail_status ){
echo "
<script>
setTimeout(function() {
window.location = 'http://www.example.com/newlocation';
}, 2000);
</script>
";
}

MDN web docs on how setTimeout works

Hope it helps!

How can I redirect a php page to another php page?

<?php
header("Location: your url");
exit;
?>


Related Topics



Leave a reply



Submit