PHP Sessions not working

software development

#1

I am, admittedly, a novice with PHP and am trying to learn sessions. I have created a small test script to learn sessions:

<?PHP session_start(); $_SESSION['test_session'] = "Hello World"; echo "The variable \$test_session is $test_session"; ?>

The problem is, the variable $test_session isn’t working. It doesn’t post to the page. All I get from the echo line is "The variable $test_session is " (I have tested the variable inside and outside of the string, so it’s not how its structured in the echo call.)

I have tried the older session_register() function with the same script and it works fine, but the variable won’t pass to the next page, which script is as follows:

<?php session_start(); echo "

The content of \$test_session is $test_session

"; ?>

I have checked that I am running PHP 5.2.x for my site. Am I missing something? This is the most basic script you could do and it should work. I tested it with another host with which I have an account and the $_SESSION call works just fine there, as well as passing the variable to the following page, with the exact same script.


#2

Try this:<?php session_start(); $_SESSION['test_session'] = "Hello World"; echo "The variable \$test_session is ".$_SESSION['test_session']; ?>You must use a period to concatenate the session variable into the string. The alternative is to do this:<?php session_start(); $_SESSION['test_session'] = "Hello World"; $test_session = $_SESSION['test_session']; echo "The variable \$test_session is $test_session"; ?>Or if you have scads of session variables and you want to create global variables for all of them, you could do this (although it isn’t a very secure method):<?php session_start(); $_SESSION['test_session'] = "Hello World"; extract($_SESSION); echo "The variable \$test_session is $test_session"; ?>si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#3

That worked for fixing the original page, but it’s not carrying over to the subsequent pages.

Why is this extra step necessary when you’re already registering it with the $_SESSION[] function? Again, it works on a different Apache/PHP5 host server.


#4

If you are expecting global variables to be automatically created from session variables, then your other setup must have register_globals enabled in the php.ini file. DreamHost’s PHP 5 setup has this option disabled because it can cause serious security issues. See this wiki page for more information.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL