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']?)

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

$_SESSION['badges'] = $result['badge_id'];

Comments

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

Comments

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.

5 Comments

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.
@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.
magic quotes has nothing to do with injections at all. not a slightest connection.
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?
in fact, I wrote you whole answer, explaining that - stackoverflow.com/questions/8116281/… did you ever read it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.