Session problem |
[eluser]shinokada[/eluser]
I am reading a Codeigniter book "Professional Codeigniter". The welcome.php start session in constructor. When I click "Add to cart" button, I get error message relating to the session. Add to cart button is linked welcome/cart/4 etc. If I refresh or go back and try it, then it does not show errors any more. However if I delete cookies and repeat the above procedure, I get the same error messages. If I change index.php error reporting from error_reporting(E_ALL) to error_reporting(E_ALL & ~E_NOTICE);, errors do not appear any more. Code: error_reporting(E_ALL & ~E_NOTICE); Could anyone tell me if there is another way to solve this? And why changing error reporting solve this problem? Regards. welcome.php Code: class Welcome extends Controller { models/moders.php Code: class MOrders extends Model{ error messages. Code: A PHP Error was encountered
[eluser]überfuzz[/eluser]
You're using the session data to set $cart. Start searching in the session array. What's in it? Keep track on it as you progress throw the process you were describing.
[eluser]shinokada[/eluser]
There is nothing in Session array at the beginning. Is it a problem? If so, how can I solve it? Thanks for your reply.
[eluser]John_Betong[/eluser]
Try moving $session_start() to the first line of code in your index.php file.
[eluser]überfuzz[/eluser]
[quote author="shinokada" date="1257445854"]There is nothing in Session array at the beginning. Is it a problem? If so, how can I solve it? Thanks for your reply.[/quote] Well if you're going to use it, it is a problem. I didn't look throw your code, but from the top of my head, I'd consider two things. Code: if(isset($_SESSION['some_key'])){ /* do stuff */ } else {/* don't do stuff */}
[eluser]shinokada[/eluser]
After session_start(), I have seen other codes having $cart = $_SESSION['cart'];. (http://v3.thewatchmakerproject.com/journ...pping-cart) I still don't understand why it gives errors.
[eluser]BrianDHall[/eluser]
[quote author="shinokada" date="1257454084"]After session_start(), I have seen other codes having $cart = $_SESSION['cart'];. (http://v3.thewatchmakerproject.com/journ...pping-cart) I still don't understand why it gives errors.[/quote] The errors are caused by the fact that $_SESSION['cart'] is not a valid array element when you are trying to access it. It is the same thing as doing this, which will also throw an error: Code: $a = $b; ...because $b has not been defined. You'll need to use something like: Code: if (! empty($_SESSION['cart'])) ...as uberfuzz suggested.
[eluser]shinokada[/eluser]
Thanks Brian! I added Code: $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array(); And it works. Thanks again.
[eluser]überfuzz[/eluser]
[quote author="shinokada" date="1257461227"]Thanks Brian! I added Code: $cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array(); And it works. Thanks again.[/quote] If you're really into CI you might consider adding some CI flavour to the session handling: session - user_guide |
Welcome Guest, Not a member yet? Register Sign In |