How to redirect to another page using PHP

0 votes

I have been 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 as this is my first website. 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 output any information before using it.

The problem is that 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; 
          } 
} 
?>

Feb 18 in Others by Soham
• 8,730 points
116 views

1 answer to this question.

0 votes

You could use a function which is similar to:

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

A user should always remember that 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.

Therefore, using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start() will help you get the answer to your problem.
 

answered Feb 18 by Aditya
• 7,280 points

Related Questions In Others

0 votes
0 answers

how to get another copy of my cert

Oct 1, 2019 in Others by Loletha
• 120 points
151 views
0 votes
1 answer

How to create subnet inside a VPC in AWS using Terraform?

Hi@akhtar, You need to use aws_subnet resource inside your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,360 points
163 views
0 votes
1 answer

How to create a App using Material Design?

Hi@akhtar, You need to use the MaterialApp method ...READ MORE

answered Jul 23, 2020 in Others by MD
• 95,360 points
153 views
0 votes
1 answer

How to create AppBar using Scaffold Widget?

Hi@akhtar, You can use the Scaffold Widget to ...READ MORE

answered Jul 23, 2020 in Others by MD
• 95,360 points
171 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
526 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
9,146 views
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,660 points
2,123 views
0 votes
1 answer

Trying to call AWS API via PHP

Try using AWS SDK for PHP, Link ...READ MORE

answered Jun 6, 2018 in AWS by Cloud gunner
• 4,660 points
981 views
0 votes
1 answer

How to fix "Headers already sent" error in PHP

The functions that send and modify the ...READ MORE

answered Feb 17 in Others by Aditya
• 7,280 points
156 views
0 votes
1 answer

How to style a checkbox using CSS

In modern browsers which include Internet Explorer ...READ MORE

answered Feb 17 in Others by Aditya
• 7,280 points
41 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP