Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Question About Sessions

Posted on 2004-08-03
4
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:TaintedGod
TaintedGod earned 200 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

715 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