Solved

Question About Sessions

Posted on 2004-08-03
4
197 Views
Last Modified: 2006-11-17
I'm trying to access the $_SESSION from within a function and it is not working:

show_cart.php

<?
include('functions.php');
session_start();
//code that sets attributes in the session
//code that calls a function in functions.php
?>

functions.php

<?
function display(){
    global $_SESSION
   // can't access the session here
}
?>

How do I access the session within this function???  I'm using PHP 4.3
0
Comment
Question by:abstractionz
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:TaintedGod
TaintedGod earned 50 total points
Comment Utility
Ok, when u make a session variable, you have session_start(); at the vary top of the page and then you assign a variable liek so:

SESSION['variable_name'] = whatever; // if you are going to assign it a string then obviously use quotes around it.

Now, on the other page that you would like to read the session variable, you again need session_start(); at the top of the page and then you can do whatever you like with the variable.

EG.

echo SESSION['variable_name'];

I dont recommond you use globals with sessions, it makes things too complicated. Everything I have shown you will work with globals off.
0
 
LVL 2

Expert Comment

by:TaintedGod
Comment Utility
If you need more refference to sessions, go here: http://www.php.net/session
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 75 total points
Comment Utility
you should not need to use global $_SESSION in the function

Sessions are super globals which means they are "globally" accessible throughout the whole script

just access the variable in the function as you would normally using $_SESSION['name'];
0
 

Author Comment

by:abstractionz
Comment Utility
Thanks.  My problem was actually that the code I was using was bad code, and I fixed it.  I thought it might've had something to do with sessions but it didn't.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now