Hi Justin, are you using <?php session_start(); ?> at the top of every page
you want to keep the session?

Cheers, Joe :)


Justin Deutsch <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Hi All,
>
> I am running Apache with PHP4 as a module.  I have also written a script
(template.php) that sets up the default envrionment, like the CSS and
navigation etc.  The script also reads in a file, as defined by the ?path=
of the URL.  The file it reads in must have the sections (includes, header
and body,  as defined by /*** ***/ and /*** end ***/ pairs).  The file is
the split into these sections and along with the templated bits of the page
compile into a single string which is the executed using the eval().  Now
this all works fine, but I also want to use sessions.  The first session
from the index.html file, which is first acessed as a login page, stores the
player class fine, but when I go to the next page (player_login.html) I seem
to loose the session.
>
> You might ask why I don't just use an existing templating system, well
it's because I think that you should only have to write the template once
and not have to touch it again.  The templating systems I have come across
force you to include a file at the top and bottom of the code you are
writing.  The one down side to the way I am doing it is that I have had to
force the other file into a include, header and body section, but this is a
trivial formatting issue.
>
> Is there something I'm missing?  I have included the code for the three
scripts below.
>
> --------
> template.php
> --------
>
> <?PHP
> /*
>  * This script is the basis for a templaing engine.  The engine uses the
>  * rewrite feature of the web server to capture the Virtual URL that the
>  * user wants and maps it to this script with a paraeter.  This script
>  * then sets up the template and includes the content from the file
>  * indicated by the user.
>  *
>  * A directory structure containing tall of the content is required, and
>  * with Apache a directory to map from.
>  */
>
>
> /* Put setup here */
> require_once("site-config.php");
> require_once("html_header.php");
> require_once("html_footer.php");
> require_once("misc_function.php");
>
>
> /* Set up some defaults */
> $title = "";
> $body = "";
> $page = "";
> $file_read = false;
> $file_content = "";
> unset($content_header);
>
> /* Read the contents of a file into a string */
> function read_file($filename)
> {
> global $file_read;
> global $file_content;
> $file_content = "";
> $file_read = false;
> //print "$filename\n\n<br><br>";
> if(file_exists($filename))
> {
> $page_code = file($filename);
> foreach ($page_code as $line)
> {
> $file_content .= $line;
> }
> //print "$file_content";
> $file_read = true;
> }
> else
> {
> $file_content = '
> <h1>File Not Found</h1>
> <p>Could not find <?=$filename?>, please let the Web Administrator
> know</p>
> ';
> }
> }
>
> /* Get the index information */
>
> read_file($NAV_COLUMN);
> $navigation = $file_content;
>
> /*
>  * garentee that if the user gives no file name then they will get the
>  * index file
>  */
> if(strcmp(basename($path), "") == 0)
> {
> if(!preg_match("/\/$/", $path))
> {
> $path .= "/";
> }
> $path .= "index.html";
> }
> /* Get the Header information and the content of the body */
> $file_name = "$CONTENT_PATH/$path";
> read_file($file_name);
> if($file_read)
> {
> //print "$file_name";
>
> /* Grab the HTTP header stuff from the file. This includes any
> * session information.
> *
> * The header section is defined by
> * <*** header ***> and <*** end header ***> at the start of the
> * file.
> */
> $section = preg_split("/\s*<\*{3}\s*end\s*header\s*\*{3}>\s*/im",
> $file_content,
> 2
> );
> if(count($section) == 2)
> {
> $content_header = $section[0];
> $content_header = preg_replace("/<\*{3}\s*header\s*\*{3}>/im",
> "",
> $content_header,
> 1
> );
> $body = $section[1];
>
> /* get the includes section */
> $section = preg_split("/\s*<\*{3}\s*end\s*includes\s*\*{3}>\s*/im",
> $content_header,
> 2
> );
> if(count($section) == 2)
> {
> $include_files = $section[0];
> $include_files = preg_replace("/<\*{3}\s*includes\s*\*{3}>/im",
> "",
> $include_files,
> 1
> );
> $content_header = $section[1];
>
> }
>
> }
> else
> {
> $body = $section[0];
> }
> }
> else
> {
> $body = $file_content;
> }
>
> if(isset($include_files))
> {
> $page .= $include_files."\n";
> }
>
> $page .= '<?PHP
> /*
>  * Start of Template code
>  */
>
> /* include state management here in an if to see if it is wanted */
>
> if(defined($KEEP_STATE) && $KEEP_STATE)
> {
> session_start();
> }
> /* Set up the headers and footers */
> header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
> header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
> header("Cache-Control: no-store, no-cache, must-revalidate");
> header("Cache-Control: post-check=0, pre-check=0", false);
> header("Pragma: no-cache");
>
> /* keep state = '.$KEEP_STATE.' */
>
> ?>
> ';
>
> if(isset($content_header))
> {
> $page .= $content_header."\n";
> }
>
>
> $page .= '
> <?PHP
> /*
>  * Start of Template code section
>  */
> $header = new html_header($title);
> $footer = new html_footer();
> $header->display()
> ?>'."\n";
> //$header->display();
> //print "<h1>:$title</h1>";
>
> /*
> print "<br><stong>header</strong>";
> highlight_string($content_header);
> */
>
> $page .= '<table summary="naigation" width="100%" height="100%">'."\n";
> $page .= '<tr>'."\n";
> $page .= '  <td width="83" class="index">'."\n";
> $page .= $navigation;
> //$page .= "<span class=\"index\">fred&nbsp;</span>\n";
> $page .= '  </td>'."\n";
> $page .= '  <td>'."\n";
>
> /*
> $page .= "<br><strong>body<strong><BR>";
> highlight_string($body);
> */
>
> $page .= $body."\n";
>
> $page .= '<?$test = session_id();print "session_id = $test";?>';
> $page .= '<?print "phpid = $PHPSESSID";?>';
>
> $page .= '  </td>'."\n";
> $page .= '</tr>'."\n";
> $page .= '</table>'."\n";
>
> $page .= '<?$footer->display();?>'."\n";
>
> print eval("?>".$page);
> if($DEBUG)
> {
> ?><h1>Debuging Information Follows</h1>
> <table width="100%"><TR><TD width="83">&nbsp;</TD><TD>
> <?
> //print "<h2>Base Name = ".basename($path)."</h2>";
> /*
> * This section adds line numbers to the string so you can make
> * sense out of the eval line numbers when an error is produced.
> *
> * IT WOULD BE A VERY GOOD IDEA TO TURN THIS INTO A FUCTION FOR
> * FUTURE USE.
> */
> flush();
> ob_start();
>
> highlight_string($page);
>
> $page = ob_get_contents();
>
> ob_end_clean();
>
> $new_page = preg_split('/<br\s*\/>/s', $page, -1);
> $i = 0;
> $page = "";
> while($i < count($new_page))
> {
> $line_no = $i + 1;
> $page .= "<font color=\"black\">$line_no: </font> ".$new_page[$i]."<br
\/>";
> $i++;
> }
> /* End line number section */
> print $page;
> ?>
> </TD></tr></table>
> <?
> }
>
> ?>
>
> --------
> index.html
> --------
> <*** includes ***>
> <?PHP
> /*
>  * Put any include or require statments here
>  */
> require_once("modules/login_box.php");
> require_once("database.php");
> require_once("player.php");
> require_once("modules/new_user.php");
>
>
> ?>
> <*** end includes ***>
> <*** header ***>
> <?PHP
> /* Put any session information here and other code which must be run
>  * before the HTML is actually sent.
>  */
> global $db;
> global $player;
> global $nick_name;
> $login_failed = TRUE;
>
> $login_box = new login_box($LOGIN_ACTION);
> $title = "";
>
> if(isset($nick_name))
> {
> /* The user has attempted to login */
>
> if(isset($new_user) && $new_user)
> {
> $title = "New User Registration";
> }
> else
> {
> if(isset($register) && $register)
> {
> /* The user is submitting their details */
>
> // Check if the user id already exists.
> $query = "SELECT id FROM player WHERE nickname = '$nick_name'";
>
> $result = $db->query($query);
>
> if(DB::isError($result))
> {
> die("Checking id: ".$result->getMessage());
> }
>
> if($result->numRows() > 0)
> {
> error_message("This nick name/login already exists, please try another");
> }
>
> // Add the user.
> if(isset($nick_name) && (strlen($nick_name) > 0)
> && !preg_match("/^\s*$/", $nick_name))
> {
> $column = "nickname";
> $values = "'".trim($nick_name)."'";
> }
> if(isset($password) && (strlen($password) > 0)
> && !preg_match("/^\s*$/", $password))
> {
> $password = md5(trim($password));
> $password2 = md5(trim($password2));
> if(strcmp($password, $password2) == 0)
> {
> $column .= ", password";
> $values .= ", '".$password."'";
> }
> else
> {
> error_message("Your passwords don't match please"
> . " entre them again :$password:$password2:");
> }
>
> }
> if(isset($first_name) && (strlen($first_name) > 0)
> && !preg_match("/^\s*$/", $first_name))
> {
> $column .= ", firstname";
> $values .= ", '".trim($first_name)."'";
> }
> if(isset($last_name) && (strlen($last_name) > 0)
> && !preg_match("/^\s*$/", $last_name))
> {
> $column .= ", lastname";
> $values .= ", '". trim($last_name)."'";
> }
> if(isset($email) && (strlen($email) > 0)
> && preg_match("/^[^@]+@[^.]+\..*$/", $eamil))
> {
> $column .= ", email";
> $values .= ", '".trim($email)."'";
> }
> if(isset($street) && (strlen($street) > 0)
> && !preg_match("/^\s*$/", $street))
> {
> $column .= ", street";
> $values .= ", '".trim($street)."'";
> }
> if(isset($suburb) && (strlen($suburb) > 0)
> && !preg_match("/^\s*$/", $suburb))
> {
> $column .= ", suburb";
> $values .= ", '".trim($suburb)."'";
> }
> if(isset($state) && (strlen($state) > 0)
> && !preg_match("/^\s*$/", $state))
> {
> $column .= ", state";
> $values .= ", '".trim($state)."'";
> }
> if(isset($postcode) && (strlen($postcode) > 0)
> && !preg_match("/^\s*$/", $postcode))
> {
> $column .= ", postcode";
> $values .= ", ".trim($postcode);
> }
> $column .= ", preferedname";
> if(isset($prefered_name))
> {
> if(strcmp($prefered_name, "nick_name") == 0)
> {
> $values .= ", true";
> }
> else
> {
> $values .= ", false";
> }
> }
> else
> {
> $values .= ", false";
> }
> if(isset($list))
> {
> if(strcmp($list, "true"))
> {
> // Send an email to the list
> }
> }
>
> //Get a valid Id
> // This needs work so it won't break if we get more than max(int2)
> // users
> $query = "SELECT id FROM player ORDER BY id DESC";
> $result = $db->query($query);
> if(DB::isError($result))
> {
> die("Getting valid id:  ".$result->getMessage());
> }
> $row = $result->fetchRow();
> $id = $row[0] + 1;
> $column .= ", id";
> $values .= ", $id";
>
> // Set the user active
> $column .= ", active";
> $values .= ", true";
>
> $query = "INSERT INTO player ($column) VALUES ($values)";
> //print $query;
>
> $result = $db->query($query);
>
> if(DB::isError($result))
> {
> print "$query<br>";
> die("Inserting new player:  ".$result->getMessage());
> }
>
> }
> else
> {
> /*
> * We only need to calculate the password if the user is
> * an existing user
> */
> $password = md5(trim($password));
> }
>
> // Check the database for this user
> //session_register_var("nick_name", $nick_name);
> session_register("nick_name");
> $GLOBALS["nick_name"] = $nick_name;
> $title = $nick_name;
>
> $query = "SELECT id "
> ."FROM player "
> ."WHERE nickname = '".trim($nick_name)."' "
> ."  and password = '$password'";
>
> $result = $db->query($query);
>
> if(DB::isError($result))
> {
> die("Geting registered user:  ".$result->getMessage());
> }
>
> if($result->numRows() != 0)
> {
> $row = $result->fetchRow();
> // print "<br>$password<br>$row[0]<br>";
>
> $player = new player($row[0], $db);
> session_register_var("player", $player);
>
> // Yeah!  The user loged in sussessfully and we can give them their
> // useable page.
> $login_failed = FALSE;
>
> }
>
> if($login_failed)
> {
> $title = "Login Failed";
> }
> else
> {
> $title = "Welcome";
> }
> }
>
> }
> else
> {
> /* The user has not attempted to login */
> $title = "Login";
> }
>
>
>
> ?>
> <*** end header ***>
> <?PHP
> /* This section is where your HTML code goes and any code which can run
>  * in the HTML
>  */
>
> if(isset($nick_name))
> {
> /* The user has attempted to login */
> if(isset($new_user) && $new_user)
> {
> if(isset($password))
> {
> new_user_form($nick_name, $password);
> }
> else
> {
> new_user_form($nick_name);
> }
> }
> else
> {
> if($login_failed)
> {
> ?>
> <p>Please try loging in again.  You may have mispelled your
> nick name or password</p>
> <?PHP
> $login_box->display($nick_name);
> }
> else
> {
> ?>
> <p>Welcome to Maric's Place</p>
> <?PHP
> session_register_var("player", $player);
> }
> }
>
> }
> else
> {
> /* The user has not attempted to login */
> $login_box->display();
> }
>
> ?>
>
> ------
> player_list.html
> ------
> <*** includes ***>
> <?PHP
> /* Put any session information here and other code which must be run
>  * before the HTML is actually sent.
>  */
>  require_once('database.php');
>  require_once('player.php');
> ?>
> <*** end includes ***>
>
> <*** header ***>
> <?PHP
>
>  global $db;
>  global $player;
>  $title = "Player List";
>  $i;
>
> ?>
> <*** end header ***>
> <?PHP
> /* This section is where your HTML code goes and any code which can run
>  * in the HTML
>  */
>
> $query = "SELECT firstname, lastname, nickname FROM player";
> $result = $db->query($query);
> if(DB::isError($result))
> {
> die("Couldn't list Players: ".$result->getMessage());
> }
>
> ?>
> <table summary="Player List" border="1" align="center" cellpadding="2">
> <tr>
> <td><strong>First Name</strong></td>
> <td><strong>Last Name</strong></td>
> <td><strong>Alias</strong></td>
> </tr>
> <?PHP
> //print $player->first_name();
> $i = 0;
> while($i < $result->numRows())
> {
> $row = $result->fetchRow();
> print "<tr>\n";
> foreach ($row as $cell)
> {
> print "<td>".htmlentities($cell) ."</td>\n";
> }
> print "</tr>\n";
> $i++;
> }
> ?>
> </table>
> <?PHP
> print $player.":<br>\n";
> print $nick_name."+<br>\n";
> print session_id()."-<br>\n";
> ?>
>
> --
> Justin 'Doychi' Deutsch
> [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to