include('header.php'); $name = $_POST['name']; $score = $_POST['score']; $dept = $_POST['dept']; $MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')"); // Bind a value to our :id hook // Produces: SELECT * FROM demo_table WHERE id = '23' $MyDB->bind(':date', $date); // Run the query $MyDB->run(); header('Location:index.php'); exit;
The above code keeps giving me an issue with the redirect. The error is the following:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in /Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.
I am totally flummoxed by this. Does anyone know what I should be doing to make it work?
EDIT
header.php code:
<?php include('class.user.php'); include('class.Connection.php'); $date = date('Y-m-j'); ?> <html> <head> <link rel=StyleSheet href="css/style.css" type="text/css" media=screen> <title>Test</title> </head> <body> <div id="page">
The root cause of this error is that php redirect header must be send before anything else. This means any space or characters sent to browser before the headers will result in this error. Like following example, there should not be any output of even a space before the headers are sent.
Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.
In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.
Look carefully at your includes - perhaps you have a blank line after a closing ?> ?
This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).
Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:
Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With