Solved

Question About Sessions

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

733 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