0

I'm trying to use sessions for learning purposes but it's not working. I got a login page which passes by POST "uname" and "passw" to this page:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<?php session_start(); ?>
<head>
...
if($_POST['uname']=="admin" && $_POST['passw']=="correctpassword"){
$_SESSION['login']="ok";}    
if($_SESSION['login']=="ok"){
echo "<p>Logged in</p>";
} else {
echo "<p>Nothing</p>";
}
...
</html>

And it works until here. Then when I pass on another page:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<?php session_start(); ?>
<head>
...
if($_SESSION['login']=="ok"){
echo "<p>Logged in</p>";
} else {
echo "<p>Nothing</p>";
...
</html>

and here it doesn't work.

I'm working on MAMP and Safari.

Can someone help me please?

1 Answers1

0

place the <?php session_start(); ?> on the top of the page without any space before

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

ThomasP1988
  • 4,597
  • 3
  • 30
  • 36