How to redirect to another page in php

Topic: PHP / MySQLPrev|Next

Answer: Use the PHP header[] Function

You can simply use the PHP header[] function to redirect a user to a different page.

The PHP code in the following example will redirect the user from the page in which it is placed to the URL //www.example.com/another-page.php. You can also specify relative URLs.

If you want to redirect the users from old page to a new page on a permanent basis then also mention HTTP response code in the header[] function as shown in the following example, so that search engines transfer "page rank" from the old page to the new page.

If the status code is not specified explicitly, for instance header["Location: URL"] defaults to 302 [Found]. For temporary redirect use the HTTP status code 307.

Related FAQ

Here are some more FAQ related to this topic:

  • How to get current page URL in PHP
  • How to remove special characters from a string in PHP
  • How to extract substring from a string in PHP

I'm building a website which includes a login page. I need to redirect the user to their profile page once they've logged in successfully, but I don't know how to do that in PHP [It's my first site].

I've searched the internet and have been told that the header[] function should do the trick, but it will only work if I haven't outputted any information before using it.

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?

What options do I have? Also, what is the best practice in these instances?

EDIT: Here's my entire login.php page:

Chủ Đề