-1

I have a login page for my form where the user fills in their username and password. The user then gets redirected to lets say page 1, and the user can go to page 2 via a submit button on page 1. Both page 1 and page 2 also contain a logout button. I am able to log out from page 1, and if I'm not logged in, I cannot go to the page by typing its url in(empty session, redirect). I want to do both of those functions on page 2 but I keep getting a warning, that the header cannot be modified.

I have only been working on php for 2 months and this can be a very easy question for some of you so try not to waste both of our times telling me the same. I'd appreciate some real input though!

session_start();
// The button to go from page 1 to 2
<input type = "submit" id = "id2" name = "id2" Value = "View Call Log"/> 
if(isset($_POST['id2'])){
    echo "<script> window.open('page2.php', '_blank'); </script>";
}   
// Everything below is in page 2.php as well.
if(isset($_POST['logout'])){
        unset ($_SESSION['userid']);
        header("Location: login.php");
    }

if(empty($_SESSION['userid'])){
    header("Location: login.php");
    exit;
}

The same code works for page 1 but doesn't for page 2

Sukrit Jaie
  • 23
  • 2
  • 7
  • We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Sep 29 '17 at 19:20
  • add example code – Mohammed Ahmed Sep 29 '17 at 19:20
  • And even more code please :) – ElChupacabra Sep 29 '17 at 19:23
  • another thing, I just learnt php since one month and I learnt it in 3 days only and I build php script (website) generate random address in more than 20 countries and now I develop new forum, So my advice to you is to remember that the time you spent on any programming language don't have relation with your expert but the reading, writing, takin complicated examples to understand it is the fast way to improve your skills. I hope I helped you with this advice :) (sorry for bad english) – Mohammed Ahmed Sep 29 '17 at 19:25

2 Answers2

0

Once you sent something to user you can't modify headers. In your code you are sending html and after that you try to send headers. You can't even send single space like in that example:

 <?php /* there is a space before <?php */ ?>

Move your php code up, before html.

PS. You would know that if you tried to google "headers already sent".

ElChupacabra
  • 1,045
  • 10
  • 18
-1

try this because the userid may not be exist so when you check if it is empty it will not work so use isset also:

 session_start();
// The button to go from page 1 to 2
<input type = "submit" id = "id2" name = "id2" Value = "View Call Log"/> 
if(isset($_POST['id2'])){
    echo "<script> window.open('page2.php', '_blank'); 
</script>";
}   
// Everything below is in page 2.php as well.
if(isset($_POST['logout'])){
        unset ($_SESSION['userid']);
        header("Location: login.php");
   }

if(isset($_SESSION['userid']) || empty($_SESSION['userid'])){
    header("Location: login.php");
    exit;
}
Mohammed Ahmed
  • 423
  • 4
  • 15