0

I'm trying to create a unique session depending on a customers ID.

session_start();

$_SESSION['ORDER$customer_id']="xyz";

However, whenever I access the session, I get the same data regardless of the $customer_id.

I'm new to sessions. I'm sure I'm missing something. Can anyone point the way?

2
  • its nothing to do with sessions, it's basic string manipulation Commented Jan 28, 2011 at 2:43
  • 1
    Each person using your site has a unique session already. PHP generated a unique session identifier for each person and sent it to the user in a cookie to identify which session file is theirs on subsequent requests. Commented Jan 28, 2011 at 2:47

3 Answers 3

3

Other people have answered the exact problem, but I have a feeling that you may be misunderstanding PHP's sessions.

Sessions are unique to each person accessing your site. You won't be able to (by default) access other sessions. This means that your array keys in $_SESSION don't have to be unique across every session currently acive.

The $_SESSION superglobal stores data specific to that person accessing your site. What you probably want to do is: $_SESSION['customer_id'] = 'xyz';

I just don't see how $_SESSION["ORDER$customer_id"]="xyz"; would be useful in a real application. I could be wrong about your situation, but you said you are new to sessions, I am just remembering the mistakes I made once.

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

2 Comments

Thanks. In this case, there is only the one admin user. But I am trying to create multiple sessions on different customer accounts. Hence the need for a unique identifier for each customer.
What do you mean "multiple sessions on different customer accounts"?
1

Single quotes don't allow for the var inside the quotes. Use double quote like this,

$_SESSION["ORDER$customer_id"]="xyz";

Comments

1

You should use $_SESSION["ORDER${customer_id}"] or $_SESSION['ORDER'.$customer_id].

Comments

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.