Home Headers already sent in a simple php script
Reply: 1

Headers already sent in a simple php script

Mattia Marziali
1#
Mattia Marziali Published in 2018-02-11 06:31:34Z

This question already has an answer here:

  • Reference - What does this error mean in PHP? 30 answers
  • How to make a redirect in PHP? 27 answers

I'm having issues with my simple PHP script. I want if $_SESSION['username'] is set to redirect to welcome.php, if not, it goes to /login.php I thought it would be easy but i seem to have some issues.

INDEX.php

<?php
session_start();
if(isset($_SESSION['username']))
{
  header("Location","/welcome.php");
}
else
{
  header("Location","/login.php");
}
?>
B. Desai
2#
B. Desai Reply to 2018-02-11 06:38:44Z

Add exit after you redirect to page whenever you do it with condition. Also write header function as below

<?php
session_start();
if(isset($_SESSION['username']))
{
  header("Location: /welcome.php");
  exit();
}
else
{
  header("Location: /login.php");
  exit();
}
?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35342 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO