-1

I'm new to PHP, and I tried to research this question but maybe I'm not asking right... I can tell from other posts/instructionals that I can not user header() or setcookie() after printing out HTML...

I commented out the header "redirects" - is there an alternative to this that I can put after HTML? It's the setcookies that are failing at the moment:

what am I not seeing? I keep getting errors but my HTML is after the PHP:

if (!isset($_COOKIE["user"])) {

$sUserIdentity = $_POST["userIdentity"];
//username is not accepted as a get value
$sPassword = $_POST["password"];
$sEmail = $_POST["email"];


$cnn= odbc_connect("Driver={SQL Server};Server=$server;Database=$dbI", $user, $password);
//check to see if email account or username already used
$sql2 = "select id from i_user where email = '" . $sEmail  . "' or username ='" . $sUserIdentity . "'";
//echo $sql2 ."<br>";
$result = odbc_exec($cnn, $sql2);
$id = odbc_result($result,"id");


if ($id == ''){
    $cnnCreate = odbc_connect("Driver={SQL Server};Server=$server;Database=$dbI", $user, $password);
    $sqlCreate  = "insert into i_user (username,email,salt,active) values ";
    $sqlCreate .= "(";
    $sqlCreate .= "'" . $sUserIdentity  . "',";
    $sqlCreate .= "'" . $sEmail  . "',"; 
    $rsCreate = odbc_exec($cnnCreate, $sqlCreate);

    $sql2 = "select * from i_user where email = '" . $sEmail  . "' and username ='" . $sUserIdentity . "'";
    //echo $sql2 ."<br>";
    $result = odbc_exec($cnn, $sql2);

    $expire=time()+60*60*24*30;
    setcookie("uid", odbc_result($result,"id"), $expire);
    setcookie("user", odbc_result($result,"username"), $expire);

    if ($rsCreate){ 
        $sMsg = "congratulations " . $_COOKIE["user"] . " and welcome "; 
    } 
    else { 
        $sMsg = "There was an error with the query"; 
    }  
}else{
    $sMsg =  "User " . $id . " already in DB";
    //header('Location: ../fec/createuser.php?error=id');
}
}
//echo $sqlCreate;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Form</title>
<script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.10.2.custom.min.js"></script>
<link rel="stylesheet" href="../css/styles.css" type="text/css">
<style>
</style>
</head>

<body>


</body> 
</html>
tree
  • 728
  • 3
  • 12
  • 22
  • if you get errors before header() it has the same effect like printing html before header() – steven May 30 '13 at 20:31
  • the only error i get is on the setcookies() - it's the first and only error – tree May 30 '13 at 20:32
  • 1
    maybe you printed out some whitespaces before setcookies(). Every output is forbidden before modify headers, maybe in other files loaded before – steven May 30 '13 at 20:33
  • I think you are right. Although there was no visible space. I had the file saved as UTF-8 encoded - and when I viewed it as Hexidecimal, there was something before the – tree May 30 '13 at 20:41

2 Answers2

1

Make sure <?php is at the very beginning of the file, with no blank lines before it. And you can't print or echo anything before calling setcookie().

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Why don't you use output buffering?

<?php

    ob_start();

?>
<html>
<body>
...
</body>
</html>
<?php

    ob_end_flush();

?>

This will put any output into a buffer and you can modify header information as long as the buffer isn't flushed. This will also avoid problems in case a PHP warning or error message was sent to output before you write to the header.

Daniel P
  • 471
  • 3
  • 9