LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2004, 02:28 AM   #1
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Question on register_globals in PHP


Does this setting needs to be on or off in PHP.ini? I heard that there are a lot of security issues if register_globals is on but then I need to use variables across multiple PHP files for a session. How do I achieve this without having register_globals on?

I read the PHP manual but the $_SESSION doesn't appear to work across multiple files. Any clues or ideas as to how to overcome this problem?
 
Old 11-22-2004, 05:55 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You have to put :
session_start()

at the top of each php scripts that use session, so $_SESSION become available. Make sure
you correctly set your session variables too...
PHP Code:
session_start();
session_register("username");
$_SESSION["username"] = "bill"
and on another page for example
PHP Code:
session_start();
echo 
$_SESSION["username"]; 
 
Old 11-22-2004, 07:41 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Well, I used session_start () on the top of the page:

Listing of firstpage.php
PHP Code:
<?php session_start (); ?>
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="secondpage.php?<?php print SID ?>">
<INPUT TYPE="text" NAME="username">
<INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="GO!">
</BODY>
</HTML>
Listing of secondpage.php
PHP Code:
<?php
session_start 
();
// ...
// ...
if (isset ($_SESSION['username'])
   print 
"User name is " $_SESSION['username'];
if (isset (
$_SESSION['age'])
   print 
"User age is " $_SESSION['age'];
?>
The above code does not work. I pass the session ID as a query string when I link to the other page so this code is supposed to work, but I get only a blank output.

The PHP manual says that session_register () functions are deprecated or outdated and I'm asked to use only $_SESSION for accessing session state variables. How do I get around this problem?

Last edited by vharishankar; 11-22-2004 at 07:42 AM.
 
Old 11-22-2004, 10:37 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
I don't see the code where you assign value in $_SESSION['username'], try to change the
second script to something like :
PHP Code:
<?php
session_start 
();
// ...
// ...

// assign the value from the field username
if(isset($_POST["username"])) {
   
$_SESSION['username'] = trim($_POST["username"]);
} else {
    
$_SESSION['username'] = "not set";
}
?>
<a href="thirdpage.php">click here</a>
and thirdpage.php :
PHP Code:
session_start (); 
echo 
$_SESSION['username']; 
[EDIT]
After went to www.php.net, I decided to not use session_register no more

Last edited by Cedrik; 11-22-2004 at 11:27 AM.
 
Old 11-22-2004, 08:23 PM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Hey thanks. $_POST works fine. Never thought of that though. So to use form fields, I use the $_POST superglobal and then assign it to a session variable.

Don't know how I missed that...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php question kwickcut Mandriva 10 12-05-2004 07:32 PM
PHP and IIS qith register_globals Dax_wells Linux - Software 0 11-04-2004 08:49 AM
question about php citrus Linux - Software 1 05-03-2004 07:24 AM
Another PHP question pkoebbe Debian 2 07-14-2003 07:06 PM
Another register_globals question raypen Programming 1 06-12-2003 07:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration