marmozzz Posted August 1, 2007 Share Posted August 1, 2007 hi i want to ask about script that i made.. it is about making cart using session here the scripts i summarize the script become like this: <?php session_start(); $menu=$_REQUEST["menu"]; $basket=$_REQUEST["id"]; if($basket!=""){ $_SESSION["basket"][$basket]=1; } $arrbasket=$_SESSION["basket"]; $count=count($arrbasket); echo $count; ?> <br> <br> P001 <a href="index.php?id=P001">Add</a><br> P002 <a href="index.php?id=P002">Add</a><br> P003 <a href="index.php?id=P003">Add</a><br> P004 <a href="index.php?id=P004">Add</a><br> <a href="index.php?menu=basket">show</a> <br> <br> <?php if($menu=="basket"){ echo "amount =$count<br>"; print_r($arrbasket); } ?> nah here comes the problem and i do not know the problem.. i am using PHP 5.1.2 and the scripts is OK, there is no problem... but when I upload to my hosting and it user PHP 5.2.1 the session is error.. so when i add the id to the session it is done, but when i click show the session seems to be gone the array in the session is gone exactly.. Please if you have any suggestion.... thanks before... Quote Link to comment https://forums.phpfreaks.com/topic/62789-need-helpmy-script-didnt-work-in-php-521/ Share on other sites More sharing options...
wildteen88 Posted August 1, 2007 Share Posted August 1, 2007 Try: <?php session_start(); // you should always check whether client defined variables (eg: _GET, _POST, _COOKIE, _SESSION, _REQUEST etc) // exists before using them if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) { $basket = $_REQUEST['id']; // check that the id has not already been added if(!isset($_SESSION['basket'][$basket])) { // not been add // we'll add the id to the basket session $_SESSION['basket'][$basket] = 1; // count how many items are in the basket session array echo count($_SESSION['basket']); } else { // id already added so we just notify the user echo $basket . ' - Already added'; } echo '<hr>'; } ?> P001 <a href="?id=P001">Add</a><br /> P002 <a href="?id=P002">Add</a><br /> P003 <a href="?id=P003">Add</a><br /> P004 <a href="?id=P004">Add</a><br /><br /> <a href="?menu=basket">show</a> <?php // again we check whether the variabe exists first before using it // and that _REQUEST['menu''] is set to basket if(isset($_REQUEST['menu']) && $_REQUEST['menu'] == 'basket') { // count how many items are in the basket session array $count = count($_SESSION['basket']); // echo out the no. of items echo '<hr><br />amount = ' . $count; // show whats in the basket session echo '<pre>' . print_r($_SESSION['basket'], true) . '</pre>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62789-need-helpmy-script-didnt-work-in-php-521/#findComment-312664 Share on other sites More sharing options...
marmozzz Posted August 2, 2007 Author Share Posted August 2, 2007 yups, thank you very for much for the scripts.. I have learned much from your scripts i have try it offline and there is no problem, but still when i upload it the amount is always 1 you can check it in this link (if you do not mind) http://www.kota-jababeka.com/tes/index.php i wonder is it because of the PHP 5.2.1 in the hosting or else? ??? thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/62789-need-helpmy-script-didnt-work-in-php-521/#findComment-313657 Share on other sites More sharing options...
wildteen88 Posted August 2, 2007 Share Posted August 2, 2007 Looks like your site has a setting called regsiter_globals on. See if you can disable this setting as it is affecting your sessions. When register_globals is on PHP automatically registers variables from user data, eg instead of using $_SESSION['basket'] you can just use $basket straight away. Having regsiter_globals on can cause security exploits within your code. For now though change all $basket variables to $basket_id and your script should function as normal. But you should still try and get register_globals disabled. Quote Link to comment https://forums.phpfreaks.com/topic/62789-need-helpmy-script-didnt-work-in-php-521/#findComment-313864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.