0

I currently have this : require_once('auth.php');

auth.php:

    session_start();

    if(!isset($_SESSION['SESS_MERCHANT_ID']) || (trim($_SESSION['SESS_MERCHANT_ID']) == '')) {
        header("location: login-form.php");
        exit();
    }

mybadges.php:
        $mybadges = mysql_query("SELECT badge_id
        FROM badges WHERE merchant_id = $current_userid ORDER BY badge_id DESC"); 

    while ($result = mysql_fetch_array($mybadges)){

    $badge_id = $result['badge_id'];

    }

I wanted to know how I can store $result['badge_id']; in a $_SESSION array (like $_SESSION['badges']?)

re1man
  • 2,337
  • 11
  • 38
  • 54

4 Answers4

1

a more sensible version of 'auth.php'

session_start();
if(empty($_SESSION['SESS_MERCHANT_ID'])) {
    header("location: login-form.php");
    exit();
}

a more sensible version of mybadges.php:

$sql = "SELECT badge_id FROM badges WHERE merchant_id = $current_userid ORDER BY badge_id DESC"
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql); 

in case there is only one bagde_id to store:

$row = mysql_fetch_array($res);
$_SESSION['badge'] = $row['badge_id'];

in case there are many id's (as one cannot say for sure from your code, what you need):

$badges = array();
while ($row = mysql_fetch_array($res)) {
    $badge_ids[] = $row['badge_id'];
}
$_SESSION['badges'] = $badge_ids;

in general, to store anything an a session, just assign that anything to a session variable:

$_SESSION['badges'] = $badge_ids;

not a big deal.

a SESSION array become exectly what you wrote. $_SESSION['badges'] is a regular variable to use.

Note that you can save only scalars, arrays and objects. Not resources.

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

If I understand well your needs, you can simply do this :

$_SESSION['badges'] = $result['badge_id'];
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

php sessions support multiple dimesion arrays.

$members=array();
foreach(mysql_fetch_array($memberresultset) as $key->$value)
$members[]=$value;

foreach($members as $mv)
$_SESSION["MEMBER"][]=$mv;

would work, for example.

Also something like this:

$_SESSION["badgeidnumbers"][]=$result["badgeid"]

But your would need to add them with foreach command, fetching more than one rows.

Extra brackets are for adding to the array using the new index number. you can write anything in the second brackets.

you also use some format like

$_SESSION["badgeidnumbers"][$result["badgeid"]]=$result["badgename"]

I do not recommend dividing your project into too many files. Normally, you shouldn't need session variables. Don't hesitate to ask ideas about the general structure of your application. It may save you a lot of time.

Uğur Gümüşhan
  • 2,455
  • 4
  • 34
  • 62
  • Friend Ugur. Sessions in general has nothing to do with number of project files. It is often used to pass the data to the same script. I hope you solved all your injection problems as you got time to give out some advices of yours. – Your Common Sense Nov 19 '11 at 05:43
  • @Col.Shrapnel at least we both agree on not using session arrays. I actually dont have injection problems, my hosting provider keeps magic quotes on. – Uğur Gümüşhan Nov 19 '11 at 07:31
  • magic quotes has nothing to do with injections at all. not a slightest connection. – Your Common Sense Nov 19 '11 at 08:34
  • it says yoiu have to turn it off. so, all your magic quotes business is just turning it off. that's all. you are turning it off far before you start thinking of injections. you have to turn it off even when no SQL processing at all. Got it? – Your Common Sense Nov 19 '11 at 16:32
  • in fact, I wrote you whole answer, explaining that - http://stackoverflow.com/questions/8116281/when-i-escape-all-the-input-sometimes-it-leaves-slashes-in-the-string-and-i/8137375#8137375 did you ever read it? – Your Common Sense Nov 19 '11 at 18:02
0

Make sure that mybadges.php includes auth.php otherwise you won't have a session started. I'm assuming that's what you have but it doesn't show up that way in the code above. Assuming you have auth.php included, just do as sputnick said with

$_SESSION['badges'] = $result['badge_id'];
TheOx
  • 2,208
  • 25
  • 28