Solved

Question About Sessions

Posted on 2004-08-03
4
199 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
ID: 11710522
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
ID: 11710535
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
ID: 11710979
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
ID: 11718990
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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 …

775 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