Viewed   77 times
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">

 Answers

1

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:

Wednesday, October 19, 2022
3

Try:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);


flush();
header("Location: http://www.website.com/");
die('should have redirected by now');

See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.

Edit:

Also try putting flush() right above your header() call.

Friday, October 28, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
5

Why not use a meta redirect tag, or a javascript solution?

HTML: <meta http-equiv="refresh" content="0;url=http://www.site.com/cart.php">

JavaScript #1: <script>window.location = "http://www.site.com/cart.php";</script>

JavaScript #2: <script>window.navigate("http://www.site.com/cart.php");</script>

Thursday, October 20, 2022
2

In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:

session_start(); 
 if (isset($_SESSION['color'])) {
            header('Location: http://mydomain.com/thankyou.php');
    }
 else {
 // Something else....

EDIT:

Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:

$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){  // separated out into a function so it wouldn't redirect before the session variable was saved
    session_write_close();
    header("Location: http://mydomain.com/thankyou.php");
}

function setColor($color){
    @session_start();
    $_SESSION['color']='blue';
    return true;
}

Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.

Sunday, December 18, 2022
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :