Jump to content

Recommended Posts

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...

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/62789-need-helpmy-script-didnt-work-in-php-521/
Share on other sites

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>';
}

?>

yups, thank you very for much for the scripts..

 

I have learned much from your scripts ;D

 

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

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.

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.