Strange string behaviour

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Strange string behaviour

Post by mjseaden »

Hi There,

For some reason, the following code:

Code: Select all

if(isset($_GET['a']))
{
    // First make some checks
    $agents = explode('|', $_SESSION['agents']);

    if(count($agents) == 3)
    {
        echo '<FONT COLOR="#FF0000">Only a maximum of 3 agents may be referred the client.</FONT>';
    }
    else
    {
        // Add to cookie variable
        if($_SESSION['agents']=='')
        {
            $_SESSION['agents'] = $_GET['a'];
        }
        else
        {
            $str = array();
            $str[] = $_SESSION['agents'];
            $str[] = '|'.$_GET['a'];
            $_SESSION['agents'] = implode("",$str);
        }
    }
}

echo '['.$_SESSION['agents'].']';
On the first run of the script with the 'a' parameter in the URL, it runs fine and sets $_SESSION['agents'] to 'a'.

However, on the second run, it sets the $_SESSION variable to 'Array|'.$_GET['a']

In other words, while it adds $_GET['a'] onto the end of $_SESSION['agents'] like I'd expect, the first element gets changed into 'Array'. Not sure why - any ideas?

Many thanks

Mark


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

try doin this

Code: Select all

$str = implode("|", $agents);
$str .= '|'.$_GET['a'];
$_SESSION['agents'] = $str;
Post Reply