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, 2022 in Others by Soham
• 9,700 points
4,695 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, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
1 answer

How to create Dropdown list in excel using php

Try this: $objValidation = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation(); $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST ); $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
1,808 views
0 votes
1 answer

How to create page borders using Apache POI in excel files with Java?

Microsoft Excel cannot do this. Libreoffice Calc ...READ MORE

answered Dec 13, 2022 in Others by narikkadan
• 63,420 points
731 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
1,198 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
10,973 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,670 points
3,765 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,670 points
1,487 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, 2022 in Others by Aditya
• 7,680 points
6,628 views
0 votes
1 answer

How to style a checkbox using CSS

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

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