Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to another page using PHP

Tags:

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:

<?php   session_start();   echo "<!DOCTYPE html>    <html>       <head>          <meta charset='utf-8'>          <title>Sprout</title>     <link rel='stylesheet' href='stylesheet.css' type='text/css'>      </head>  <body>     <div class='box'>     <form action='login.php' method='post'>        Name<br /> <input type='text' name='username' class='form'/><br />        Password<br /> <input type='password' name='password' class='form'/>        <input type='submit' value='Login' class='button' />     </form>     </div>  </body>   </html>";  if ($_SERVER['REQUEST_METHOD'] == 'POST')  {     $username = $_POST["username"];     $password = $_POST["password"];      $dbhost = "localhost";     $dbuser = "root";     $dbpass = "root";      $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");      $dbname = "database";      mysql_select_db($dbname);      $query = "SELECT username FROM users WHERE username = '$username' AND password = '$password'";      $result = mysql_query($query) or die ("Failed Query of " . $query);       while($row = mysql_fetch_assoc($result))     {             $_SESSION["user"] = $username;     } } ?> 
like image 691
jasonaburton Avatar asked Feb 02 '11 07:02

jasonaburton


People also ask

How do I redirect to another page?

How to Redirect to Another Page in HTML. To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page. The <head> section of an HTML document contains metadata that is useful for the browser, but invisible to users viewing the page.

Which function is used to redirect a page in PHP?

The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client).

How do I redirect to another page in PHP w3schools?

Redirecting Browserphp header("Location: http://www.example.com/"); ?> The following command will redirect the browser window to the given location as soon as the command is executed. Please note that Location starts with capital L, some browsers might not redirect if small l is used.


1 Answers

You could use a function similar to:

function redirect($url) {     ob_start();     header('Location: '.$url);     ob_end_flush();     die(); } 

Worth noting, you should always use either ob_flush() or ob_start() at the beginning of your header('location: ...'); functions, and you should always follow them with a die() or exit() function to prevent further code execution.

Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/

This guide includes reasons for using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start(), and some potential errors that the others answers have left out at this point.

like image 158
Joe Meyer Avatar answered Oct 05 '22 02:10

Joe Meyer