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:
<?
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.
<A HREF="index.php?p=view_match&m=<? echo $details['match_id']; ?>"><? echo $details['match_id']; ?></A>
View_match.php:
<?
// 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.
Edit: added PHP code highlighting