Jump to content

[SOLVED] PHP session and header redirect problem... HMMmmm


bri0987

Recommended Posts

Here is the code for the top of the "customise.php" page.

<?php require_once('../Connections/DM_database.php'); ?>
<?php include_once("../includes/functions.php"); ?>
<?php 
if (isset($_GET["Product_ID"])) { $ProductID = $_GET["Product_ID"];
} elseif (isset($_POST["Product_ID"])) { $ProductID = $_POST["Product_ID"]; $_GET["Product_ID"] = $ProductID;
} else { $_GET["Product_ID"] = "1"; $ProductID = $_GET["Product_ID"]; 
} 
?>

<?php
if (isset($_POST['AddToCart'])) {
$qty = "1";
$newstring = "";
$i = 1;
	foreach ($_POST["Component_ID"] as $type => $component) { 
		if ($i == 1) {
		$newstring = $component;
		++$i;
		} else {
		$newstring = $newstring . ", " .$component;
		}
	}
		$query = "INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`) VALUES ('" . GetCartId() . "', " . intval($ProductID) . ", '" . $newstring . "', " . intval($qty) . ")";
		mysql_query($query) or die("query='$query '<br>".mysql_error());

header("Location: cart.php");
} else {

 

The function "GetCartId()" does the following:

 

<?php
function GetCartId() 
{ 
if(isset($_COOKIE["sc_ID"])) { 
	return $_COOKIE["sc_ID"]; 
} else { 
	session_start(); 
	setcookie("sc_ID", session_id(), time() + ((3600 * 24) * 7)); 
	return session_id(); 
} 
} 
?>

 

The Error message I got when I first try to submit the form ... With no seesion or cookie set:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\shopping_cart\shopping_cart\customize.php:10) in C:\wamp\www\shopping_cart\includes\functions.php on line 12

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\shopping_cart\shopping_cart\customize.php:10) in C:\wamp\www\shopping_cart\includes\functions.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\shopping_cart\shopping_cart\customize.php:10) in C:\wamp\www\shopping_cart\includes\functions.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\shopping_cart\shopping_cart\customize.php:10) in C:\wamp\www\shopping_cart\shopping_cart\customize.php on line 26

 

 

 

The error message I get after I try the process again is:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\shopping_cart\shopping_cart\customize.php:10) in C:\wamp\www\shopping_cart\shopping_cart\customize.php on line 26

Link to comment
Share on other sites

?>

<?php

 

Any blank spaces counts as output, if there are any in the include files remove them. ALso you do not need to go in and out of PHP soo many times. This would suffice

 

<?php 
require_once('../Connections/DM_database.php'); 
include_once("../includes/functions.php"); 

if (isset($_GET["Product_ID"])) { $ProductID = $_GET["Product_ID"];
} elseif (isset($_POST["Product_ID"])) { $ProductID = $_POST["Product_ID"]; $_GET["Product_ID"] = $ProductID;
} else { $_GET["Product_ID"] = "1"; $ProductID = $_GET["Product_ID"]; 
        }

if (isset($_POST['AddToCart'])) {
$qty = "1";
$newstring = "";
$i = 1;
	foreach ($_POST["Component_ID"] as $type => $component) { 
		if ($i == 1) {
		$newstring = $component;
		++$i;
		} else {
		$newstring = $newstring . ", " .$component;
		}
	}
		$query = "INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`) VALUES ('" . GetCartId() . "', " . intval($ProductID) . ", '" . $newstring . "', " . intval($qty) . ")";
		mysql_query($query) or die("query='$query '<br>".mysql_error());

header("Location: cart.php");
} else {

 

And I am talking about blank spaces outside of the php tags btw. That should work, given the same situation does not happen in your include files.

Link to comment
Share on other sites

An easy way to work around header problems is to use output buffering.  Like premiso said, header errors tend to pop up because something - even whitespace - is output from your script before you make the header call.  If you buffer your output, then you really don't have to worry about when your output is sent, as you'll have direct control over it.

 

The easiest way to do it is to merely put

ob_start();

as the very first line of code in your script.  This initializes the buffer. 

 

At the end put

ob_end_flush();

which sends everything stored in the buffer to the screen.

 

Link to comment
Share on other sites

=) The only issue with output buffering is efficiency and it is considered sloppy/poor programming. The best way is to do it is do it correctly and use items as they are intended to be used.

 

It is an alternative option, but in my opinion it is not an option for my reasoning above. When you get into much larger scripts you will see why the output buffer option is half-assed.

Link to comment
Share on other sites

=) The only issue with output buffering is efficiency and it is considered sloppy/poor programming. The best way is to do it is do it correctly and use items as they are intended to be used.

 

It is an alternative option, but in my opinion it is not an option for my reasoning above. When you get into much larger scripts you will see why the output buffer option is half-assed.

 

I tend to use it as a failsafe, only.  I always go the validate/process input --> output (if necessary) route.

Link to comment
Share on other sites

It is interesting to see the other's posts.

 

Javascript redirects are unnecessary if you use proper coding or if they are required for a specific reason (which I cannot think of right now).  How sites should be coded, using my own experience, is by storing all output into a string variable and printing all the output at once, which will avoid any header issues. Simply stated here, he just needs to remove the whitespace. The output buffer is overkill as it will slow down the process of the script and potentially tie up some memory on the server and if your website has a lot of traffic it will cause speed issues later down the line; Which is why I say it is inefficient. I have tested my theory and proven that using output buffer is a lot slower than just printing the data and doing everything like it should be, and not putting a band-aid on an open wound.

 

Solve the problem at it's core and you will have less problems later on. The output buffering is handy in certain situations, for example using GZip compression, but using it because you do not want to go back and remove whitespaces and making sure your script is not outputting before header calls is half-assed and just lazy and chances are you will eventually go back and fix it at some point, so might as well take care of the issue now instead of a few months later after more code depends on the band-aid and a whole new structure has to be written.

 

Again this is just from personal experience and my own opinion from what I have encountered in my 10 years of programming. It is better to do something right the first time then have to fix it two to three more times. Efficiency is key here in more than just the code itself, but your time as well.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.