PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 28th, 2005, 05:20 PM
PHPVortex PHPVortex is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 1 PHPVortex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 13 sec
Reputation Power: 0
Message-driven game: sessions variables and include problems

Hello all,

I have a question regarding a coding problem that I can't seem to fix. I am creating a message-driven RPG site. Coding PHP is a new hobby, so I apologize if not all code is as optimal as it could be .

The site is based around a template page (index.php). It contains a navigation menu and loads content by including the required pages. Default content is an introduction page. Also index.php facilitates user authentication, by means of a login form.

The coding problem relates to session variables being lost when browsing around the site.

index.php -> all_matches.php -> view_match.php -> add_post.php

The player logs in on index.php (a login form is located there), then browses to all_matches.php where he selects a match where he wants to post. In view_match.php, the selected match is displayed. I want to display a hyperlink in view_match.php to an 'add post form' (add_post.php). But this hyperlink may only be displayed if the player is logged in and is authorized to post in the match. Of course, I later check this again in add_post.php for security reasons.

Index.php creates a session with the $Name (user name), $access (allowed access) and $login (set to 1 if logged in). Because the template page stays the same, the data is retained. When going to all_matches, session data is retained. But when I go to view_match, session data is lost.

Remember that all_matches.php, view_match.php and add_post.php are all loaded as content in index.php!

If anyone knows how to solve this problem, I am eternally grateful. Note that I use session_register, instead of $_SESSION. This isn't the problem as on the other pages it works. But I'll probably change this in the future. Some excerpts of the scripts are below.

Index.php:
==========

PHP Code:
<?
session_start
();
 
// Specify the base URL for view_match.php:
$b_url='http://www.mysite.org/';
 
// Get the desired page from the URL if provided, otherwise open introduction page as default:
$page$_GET['p'];
 
// Get the desired match to display from the URL if provided:
$mid$_GET['m'];
 
... 
// Display basic layout in HTML.
... // If the player is logged in, the index.php will display "You are logged in as ...".
 
// Display the desired page in the main table of the HTML layout.
switch($page) {
case 
"intro":
include 
'intro.php';
break;
case 
"all_matches":
include 
'all_matches.php';
break;
case 
"view_match":
include 
$b_url.'view_match.php?m='.$mid;
// I use this because include() can only use URL variables with absolute URL's.
break;
... 
// More options.
default: 
include 
'intro.php';
}
?>
... // Display the HTML navigation bar. One of the links is:
<A HREF="index.php?p=all_matches">View All Matches</A><BR>
...
<?
// Display the login form on the page and execute the following code if authentication is succesfull:
// Register login variables:
$login "1";
$access $l_info['access'];
 
session_register("login");
session_register("Name");
session_register("access");
 
// Redirect back to index.php:
session_write_close();
$insertGoTo="http://www.mysite.org/index.php?p=intro";
 
print 
"<script language=\"JavaScript\">";
print 
"window.location = '$insertGoTo' ";
print 
"</script>";
exit();
?>


All_matches.php:
================

This page gives an overview of all available matches. The only important thing here is the link to view_match.php. The variable $details['match_id'] is retrieved from a MySQL database.

PHP Code:
<A HREF="index.php?p=view_match&m=<? echo $details['match_id']; ?>"><? echo $details['match_id']; ?></A> 


View_match.php:
===============

PHP Code:
<?
// Get the desired match to display from the URL if provided:
$mid$_GET['m'];
 
... 
// Display match posts.
 
// Check if the player may post in this match. THIS IS WHERE THE SESSION VARIABLES ARE LOST!!!
echo $Name;
echo 
$login;
echo 
$access;
if (empty(
$access)) {
echo 
"No access specified!";
} else {
echo 
"Access specified!";
}
 
... 
// Some further HTML to display the rest of the page.
?>


Conclusion:
===========
I always get "No access specified!" in view_match.php, I don't know why. Perhaps, this is because the code [include $b_url.'view_match.php?m='.$mid;] in index.php is an absolute URL and PHP drops the session for security reasons ???? Any help is appreciated. Thanks for reading this far.

Reply With Quote
  #2  
Old February 4th, 2005, 07:03 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2Folding Points: 908096 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Ever since they included the new 'superglobals', I've stopped using functions like 'session_register'
A much easier, and in my experience reliable, way to handle session vars is simple store and retrieve them from the $_SESSION[] superglobal.
If you want to store something in your session:

$_SESSION['varname'] = $varname;
or
$_SESSION['varname'] = value;

to retrieve them:

$varname = $_SESSION['varname'];

A note on this: if register_globals is enabled on your server, the 'retrieval code' is not necesary. However, for security reasons, it is very unwise to rely on this. (Depending on server settings, people may overwrite your session variables with GET variables). This is also probably where your code goes wrong: You assume $Name, $login and $access are registered automatically, but when register_globals is off, this may give trouble.

Personally, I never even use local versions of session variables. Whenever I need a variable that's in a session, I use the $_SESSION variable there.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Last edited by Itsacon : February 4th, 2005 at 07:06 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Message-driven game: sessions variables and include problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT