0

I'm trying to unset a certain value in a session array in php. I would like to ask if there's a better way in doing this:

    <?php

    session_start();


    if(isset($_GET['Uname'])){
        echo "Uname is set!";


            $uname=$_GET['Uname'];


        echo count($_SESSION['user']);

        for($x=0; $x < count($_SESSION['user']); $x++ ){

            if($_SESSION['user'][$x]['Uname']==$uname){

                unset($_SESSION['user'][$x]['Uname']);
            }

        }


    }else{


    }

?>

Is it possible to accomplish the same thing using a foreach loop?Or another method

Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • 2
    Looks like you doesn't understood $_SESSION's fundamentals yet. http://stackoverflow.com/questions/5403721/how-to-loop-through-session-array-in-php One. Session. Per. User. Also you may want to meet session_destroy: http://www.php.net/manual/en/function.session-destroy.php – fabrik Mar 24 '11 at 09:41

2 Answers2

0

Sure unsetting user should solve that. You don't need a loop. Try this, refreshing page will surely set value at one time and other unset it's value.

<?php

session_start();

$array = array('arr', 'arr', 'arr', 'arr', 'arr', 'arr');

if(isset($_SESSION['user']))
{
    print_r($_SESSION['user']);
    unset($_SESSION['user']);   
}
else{
    $_SESSION['user'] = $array;
    echo "user session was set";
}

And according to this question, https://stackoverflow.com/questions/4891301/top-bad-practices-in-php , using count() in loop is a bad practice.

Community
  • 1
  • 1
S L
  • 14,262
  • 17
  • 77
  • 116
0

I'm trying to unset a certain value in a session array in php. I would like to ask if there's a better way in doing this.

I can assure you that the best way to unset a variable is to use unset() function on it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345