Solved

Question About Sessions

Posted on 2004-08-03
4
202 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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