Christopher Bergin Posted March 4, 2013 Share Posted March 4, 2013 I couldn't get my sessions to work while trying to replicate the exercise in this chapter so I attempted to pass values using a URL. I ran into problems when trying to pass an object value, so I serialized the value and tried to append it onto the URL. The error I'm receiving is the following: Warning: Header may not contain NUL bytes in PHP test/login.php on line 31. Can anyone offer insight regarding the error? // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form submission // Validate the form data: if ($form->validate()) { // Check against the database: $q = 'SELECT id, userType, username, email FROM users WHERE email=:email AND pass=SHA1(:pass)'; $stmt = $pdo->prepare($q); $r = $stmt->execute(array(':email' => $email->getValue(), ':pass' => $password->getValue())); // Try to fetch the results: if ($r) { $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $user = $stmt->fetch(); } // Store the user in the session and redirect: if ($user) { // Store in a session: //$_SESSION['user'] = $user; // Redirect: $user = serialize($user); header("location:index.php?user=$user"); exit; } } // End of form validation IF. Link to comment Share on other sites More sharing options...
Larry Posted March 6, 2013 Share Posted March 6, 2013 Did you do a var_dump($user) to see what it's value is after serialize()? Link to comment Share on other sites More sharing options...
Christopher Bergin Posted March 8, 2013 Author Share Posted March 8, 2013 no I didn't. I did a print_r{$user} and it confirmed that the object was converted to a string. I was able to pass the string in a standard <a href> tag as the values appeared in the URL. So the problem seemed to be with the header function. Anyway, once I passed the serialized object to another page, the script failed to unserialize it so I abandoned the idea of passing an object in a URL and simply appended the id value to a link and instantiated a new object on the other page using a $_GET['id'] value as a reference. I was still able to adhere to the MVC paradigm. Link to comment Share on other sites More sharing options...
Larry Posted March 8, 2013 Share Posted March 8, 2013 Hello Christopher. I'm glad you've got a solution you're happy with. It's not a good sign when a Google search on this error turns up this forum thread on page 2 of the results! My assumption is that PHP put a NULL byte at the end of the serialized object to terminate it. In theory, using trim() on the serialized string should remove it and make it safe to use in header(). In theory. But more importantly, glad you've got some solution. Link to comment Share on other sites More sharing options...
Recommended Posts