Q:

php if/else not working as expected

I am new to PHP and cannot figure out why this is not working properly. What I want to happen is if a session is found execute the web page otherwise redirect to login.php. What is happening is the webpage is being executed and the user sees sql errors then the redirect happens. It's as if both the if and the else are being executed. Please help.
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>

The entire webpage is in here but removed it for a short example.

</body>
</html>

<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">'; 
}
?>

A:

a nicer way:
<?php
session_start();
if (!isset($_SESSION['userID'])){
    header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);

?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>

check your braces and do session_start before using the session

