RE: [PHP] Help! Can't set cookie or redirect!!!
| From: | John W. Holmes | Date: | Sun, 29 Sep 2002 05:51:59 +0000 |
| Subject: | RE: [PHP] Help! Can't set cookie or redirect!!! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-118050@lists.php.net to get a copy of this message | ||
OK...if your error says you have output, then you have output. It's not
lying to you. Your error was this:
> > > Warning: Cannot add header information - headers already sent by
> > (output
> > > started at /users/ppowell/web/my/process.php:5) in
> > > /users/ppowell/web/my/process.php on line 76
Ok, that tells you that the "output started at ... /process.php:5", that
means line 5, which is phpinfo(), which causes output to the browser...
Lose that line...
---John Holmes...
> -----Original Message-----
> From: Phil Powell [mailto:soazine@erols.com]
> Sent: Saturday, September 28, 2002 9:16 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Help! Can't set cookie or redirect!!!
>
> Ok thanx.. yeah I knew that you can set a cookie before anything is
set to
> the browser, however, if you view my code, nothing is sent as output
> before
> the cookie is set.
>
> <?
> // Don't forget you have to use php4.cgi to call this script and
this
> script's URL will be:
> // /cgi-bin/cgiwrap/ppowell/php4.cgi/~ppowell/my/process.php
>
> phpinfo();
> $willChangeLayout = 1; $isEmptyLayoutValues = 1;
>
> $path = $DOCUMENT_ROOT . "/my/";
>
> // SERVER-SIDE VALIDATION
> foreach ($HTTP_POST_VARS as $key => $val) {
> if (strcmp($key, "isDefaultLayout") == 0) $willChangeLayout = 0;
> if (!empty($HTTP_POST_VARS[$key])) $isEmptyLayoutValues = 0;
> if (!empty($HTTP_POST_FILES['myImage']['name']))
$isEmptyLayoutValues =
> 0;
> }
>
> if ($willChangeLayout && $isEmptyLayoutValues) {
> header("Location: " . $refURL . "?errorMsg=" . urlencode("Please
fill
> out
> all required fields"));
> //--END OF SERVER-SIDE VALIDTION
> } else {
>
> // FILE UPLOAD HANDLING
>
> if (!is_dir("$path/images/")) mkdir("$path/images/", 0755);
>
> if (is_uploaded_file($HTTP_POST_FILES['myImage']['tmp_name']) &&
> !file_exists("$DOCUMENT_ROOT/my/images/" .
> $HTTP_POST_FILES['myImage']['name']) &&
> $willChangeLayout)
> move_uploaded_file($HTTP_POST_FILES['myImage']['tmp_name'], $path.
> "/images/" . urlencode($HTTP_POST_FILES['myImage']['name']));
> //--END OF FILE UPLOAD HANDLING
>
>
> // IF LAYOUT COLORS HAVE BEEN SUBMITTED COMBINE THEM INTO WORKABLE
> STRING
> TO PLACE INTO FILE
> if ($willChangeLayout) {
>
> $stuff = "";
>
> // OPEN UP FILE "layout.txt" TO GET MOST CURRENT ID, ELSE DEFAULT
TO 1
> if (file_exists("$path/layout.txt")) {
> $layoutID = fopen("$path/layout.txt", "r") or die("Could not open
> file:
> $path/layout.txt");
> $stuff .= fread($layoutID, filesize("$path/layout.txt"));
> fclose($layoutID) or die("Could not close file:
$path/layout.txt");
> // OBTAIN MAX ID BY DOING PREG_MATCH_ALL TO GET ALL PATTERNS OF
> "id=number"
> preg_match_all("/id=[0-9]+/", $stuff, $idArray,
PREG_PATTERN_ORDER);
> list($key, $id) = split("=", end($idArray[0]));
> $id++;
> } else {
> $id = 1;
> }
>
> $layoutString = "id=" . $id . "&";
>
> $varExceptionArray = array("isDefaultLayout", "submit",
> "refURL",
> "previewButton", "cancel",
> "MAX_FILE_SIZE");
> foreach ($HTTP_POST_VARS as $key => $val) {
> if (!in_array($key, $varExceptionArray)) {
> $val = preg_replace("/&/", "#", $val); // IN THEORY YOUR
EXTERNAL
> IMAGE HAS NO "&"
> $val = preg_replace("/=/", "#", $val); // IN THEORY YOUR
EXTERNAL
> IMAGE HAS NO "="
> if (strcmp($key, "myExternalImage") == 0) {
> $layoutString .= "background=" . urlencode($val) . "&";
> } else {
> $layoutString .= $key . "=" . urlencode($val) . "&";
> }
> }
> }
>
> if (!empty($HTTP_POST_FILES['myImage']['name'])) {
> $layoutString .= "background=" .
> urlencode($HTTP_POST_FILES['myImage']['name']);
> } else {
> $layoutString = substr($layoutString, 0, -1);
> }
> $layoutID = fopen("$path/layout.txt", "w") or die("Could not write
to
> file: $path/layout.txt");
> fwrite($layoutID, $stuff . $layoutString . "\n");
> fflush($layoutID); fclose($layoutID);
> // SET THE COOKIE FOR LAYOUT
> setcookie("valLayout", $id, time()+3600*24*30*12*100,
$SERVER_NAME);
> //
> WILL EXPIRE IN 100 YEARS.. UM YEAH
> } else { // $willChangeLayout IS FALSE YOU ARE USING
"isDefaultLayout"
> -
> DESTROY COOKIE
> $id = $HTTP_COOKIE_VARS["valLayout"];
> if (empty($id)) $id = 0;
> // DELETE ENTRY FROM layout.txt
> $layoutID = fopen("$path/layout.txt", r) or die("Could not open
file:
> $path/layout.txt");
> $stuff = fread($layoutID, filesize("$path/layout.txt"));
> fclose($layoutID) or die("Could not close file:
$path/layout.txt");
> preg_match_all("/id=[\n]+\n/", $stuff, $idArray,
PREG_PATTERN_ORDER);
> preg_replace("end($idArray[0])", "", $stuff);
> $layoutID = fopen("$path/layout.txt", w) or die("Could not open
file:
> $path/layout.txt");
> fwrite($layoutID, $stuff); fflush($layoutID); fclose($layoutID);
> // DELETE COOKIE
> setcookie("valLayout", "", time()-3600, $SERVER_NAME);
> }
> //--END OF LAYOUT COLOR TO WORKABLE STRING HANDLING
>
> // REDIRECT
> header("Location: " . $HTTP_POST_VARS["refURL"]);
>
> } //--END OF LAYOUT CHANGING
> ?>
>
> Perhaps I'm just not finding it. :(
>
> Phil
> "John W. Holmes" <holmes072000@charter.net> wrote in message
> news:000e01c26754$cf27c8c0$7c02a8c0@coconut...
> > You can only set a cookie before any output is send to the browser.
A
> > newline, space, or <html>, etc, is output to the browser. Redesign
your
> > code so the cookie is set before any output or use output buffering.
> >
> > ---John Holmes...
> >
> > > -----Original Message-----
> > > From: Phil Powell [mailto:soazine@erols.com]
> > > Sent: Saturday, September 28, 2002 9:03 PM
> > > To: php-general@lists.php.net; talk@nyphp.org
> > > Subject: [PHP] Help! Can't set cookie or redirect!!!
> > >
> > > I am getting the following errors attempting to set a cookie and
> > redirect:
> > >
> > > Warning: Cannot add header information - headers already sent by
> > (output
> > > started at /users/ppowell/web/my/process.php:5) in
> > > /users/ppowell/web/my/process.php on line 76
> > >
> > > Warning: Cannot add header information - headers already sent by
> > (output
> > > started at /users/ppowell/web/my/process.php:5) in
> > > /users/ppowell/web/my/process.php on line 77
> > >
> > >
> > > Lines:
> > >
> > > setcookie("valLayout", $id, time()+3600*24*30*12*100,
> > $SERVER_NAME); //
> > > WILL EXPIRE IN 100 YEARS.. UM YEAH
> > > header("Location: " . $HTTP_POST_VARS["refURL"]);
> > >
> > > The first time I ran this script it set the cookie just fine;
> > subsequent
> > > runnings of this script produce this error. What am I doing
wrong,
> > > anyone?
> > >
> > > Thanx
> > > Phil
> >
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php