-1

i have created a $_SESSION variable in a file named (cart.php) then i called this variable in another page (index.php) , it keeps showing this error : UNDEFINED INDEX ON 'NbrCmd' in index.php meanwhile it's working in cart.php this is 'cart.php' variable declaration and 'echo' :

$nbrcmd = 3;
$_SESSION['nbrcmd_online'] = $nbrcmd;

this is index.php where i called this variable :

<p class="text-shopping-cart cart-total-full"> <?php echo $_SESSION['nbrcmd_online'] ; ?> </p>

the session is already started in the two files , and the index is right ! this code : <?php echo $_SESSION['nbrcmd_online'] ; ?> works in cart.php but in index.php it's an undefined index . please help and thanks

CoderTn
  • 985
  • 2
  • 22
  • 49
  • 1
    *Note:* The `session_start()` function must be the very first thing in your document. Before any HTML tags. – A l w a y s S u n n y Aug 02 '18 at 16:15
  • 1
    Please copy/paste the _exact_ error message you are getting. – Patrick Q Aug 02 '18 at 16:16
  • If the session is started in both files, then this is an issue with undefined index in the array. Please see https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef. I imagine this is related to index.php not having the session variable set before going to cart.php. – Jonathan Aug 02 '18 at 16:17
  • Let's see the full code of the two files, please. – RDumais Aug 02 '18 at 16:49
  • full error message ( ! ) NOTICE: UNDEFINED INDEX: NBRCMD_ONLINE IN C:\WAMP64\WWW\LOOH\VIEWS\INDEX.PHP ON LINE 401 – CoderTn Aug 02 '18 at 16:52
  • Guys using var_dump($_SESSION) i figured out that the two files dosen't have the same $_SESSION variables ! it's supposed to be one session started and one session variables no ? – CoderTn Aug 02 '18 at 16:53

2 Answers2

2

Use session_start() at the top of your document.

Also use var_dump($_SESSION) to find out what exactly is set as session.

Reza Saadati
  • 5,018
  • 4
  • 27
  • 64
  • 1
    The post says "the session is already started in the two files". Barring proof to the contrary, I'd say OP is already doing what you're suggesting. – Patrick Q Aug 02 '18 at 16:19
  • @PatrickQ true, but it's not sure where exactly he has started it. – Reza Saadati Aug 02 '18 at 16:27
  • That may be, but at this point, you're really just guessing, which makes this more of a comment than a real answer. – Patrick Q Aug 02 '18 at 16:29
  • Guys when i used var dump $_SESSION it's not showing the variable that i created in cart.php !!! – CoderTn Aug 02 '18 at 16:42
  • the session is started and to check that out , i have put session_start() and it says session restarted – CoderTn Aug 02 '18 at 16:43
  • Guys using var_dump($_SESSION) i figured out that the two files dosen't have the same $_SESSION variables ! it's supposed to be one session started and one session variables no ? – CoderTn Aug 02 '18 at 16:55
  • @BechirGharsallah if `var_dump($_SESSION)` doesn't output the session with the key `nbrcmd_online`, you can be sure that either your session with the key `nbrcmd_online` could not be set or you unset the session, before you try to output the value of it. Make sure there is no `session_destroy()` in your script. – Reza Saadati Aug 02 '18 at 18:27
1

add config.php file in variable:

session_start();

oxcakmak
  • 13
  • 7
  • 1
    The post says "the session is already started in the two files". Barring proof to the contrary, I'd say OP is already doing what you're suggesting. – Patrick Q Aug 02 '18 at 16:19
  • If it is written in two files, clear cookies and cache and continue on a hidden tab if you are working on a project chrome browser. – oxcakmak Aug 03 '18 at 19:53