0

I use this piece of code for my login form.

<?php

  include('session.php');

  if(isset($_SESSION['username'])) {
    header('http://www.askmephilosophy.co.nf/account.php/');
  }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en">
  <head>
    <title>AMP : Log In</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>

  <body>
    <?php
       echo $_SESSION['username'];
    ?>
  </body>
</html>

Yet when I try to redirect it does not work. On my session.php file I have:

session_start();

$_SESSION['username'] = 'John';

Still it will echo out the name 'John', while it won't redirect to the account.php page, in the header() function.

I've used this before, but I see do something wrong. While I have the PHP code above the HTML.

Could someone explain to me why this is not working?

2 Answers 2

3

header('http://www.askmephilosophy.co.nf/account.php/'); does not mean a thing to the browser and gets ignored.

You probably want to add "Location:" to it to redirect somewhere:

 header('Location: http://www.askmephilosophy.co.nf/account.php/');
Sign up to request clarification or add additional context in comments.

2 Comments

If this solution doesn't fix your problem try adding: ob_start(); to the top of the file.
There is no evidence of "headers already sent" errors in the question, so there is no need to fix that. Also, adding ob_start() might not fix it if the space or BOM is at the start of the first script.
1

You need to fix the header you are sending and add an exit statement, so the redirect is getting through.

  if(isset($_SESSION['username'])) {
    header('Location:http://www.askmephilosophy.co.nf/account.php/');
    exit();
  }

3 Comments

The exit is not mandatory. In fact it is suggested to add a page with a manual link that points to the same location where the automatic redirect is going, just in case something goes wrong.
If you don't exit, the script continues running until it reaches the end which is not really necessary.
i dont know why they have down-voted,even if all what you said is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.