Page Confusion?

A

Anonymous

Guest
I have just deleted my first, very in detail post about what I thought the problem could've been. But I have narrowed down what I think the problem can be.

I have a function called StorePage which is called from index.php and this function saves the page name to session vars. Then on another page product_display.php which shows the course the user took to get to the page, like so...

Browse > Nokia > Nokia 7260

On the first display of the page, it works perfectly fine. But on a refresh of the page it turns into this

Home > Nokia 7260.

This is what it should like after going from the home page to product_display.php. The funny thing is, is how from index.php the StorePage function stores the same vars which are being displayed. So I played around and changed the values put into the StorePage function on index.php to see if what happens on product_display.php and sure enough whatever shows up after the refresh on product_display.php is what I put in on the values on index.php, regardless of whether or not index.php had been called in this browser session.

The only link between these two pages I can see is in a header("Location: index.php") which happens if the product asked for can not be displayed, but even with this taken out of the product_display script the same problem occurs.

My first solution was to try and put a conditional on index.php as shown below.

Code:
if ($_SERVER['PHP_SELF'] == 'index.php') {
	StorePage("Home", "index.php");
}

But even from product_display.php, this conditional returns true even though if I print out PHP_SELF from product_display.php, on any part of the script it will always print product_display.php
 
I think this problem is related to include() which i'm almost sure you're using.
I had this problem once doing the same thing, well... not with sessions, but that's not the problem.

A little example. Let's say this next code is from a file called index.php:
Code:
<?php
// index.php file

// Some file included in which will also be printed the $_SERVER['PHP_SELF'] variable.
include(functions.php);

print $_SERVER['PHP_SELF'];

?>
Now note that (and if i remember right) index.php and functions.php have a different $_SERVER['PHP_SELF'] value.

My solution was to implement a class where i define at FIRST, the main page headers and info like the main file name (in thi case index.php) and its title. I've used $_SERVER['BASE_PAGE'] variable for that.

Just a sample class:
Code:
<?php
// site.class.php file

class Site {

   // -------------------------------------------------------------------
   // Settings
   // -------------------------------------------------------------------

   // For storing the page title
   var $_pageTitle = '';

   // For storing the BASE_PAGE
   $this->_basePage = '';

   // With this simple and unique function we can do that so.
   function SetPageTitle($title, $basepage) {
      $this->_pageTitle = $title;
      $_SERVER['BASE_PAGE'] = $basepage;
   }

   // Aditional
   function GetBasePage() {
      return $_SERVER['BASE_PAGE'];
   }
   
   function GetPageTitle() {
      return $this->_pageTitle;
   }

} // End Site class

?>
Now as an exemple too, let's say i use a file called common.php where i'll do some header related stuff and include the main classes or functions' files:
Code:
<?php
// common.php file

// Just some stuff
if ( !defined('IN_SITE') ) {
	die('Are you trying to hack me dude !?');
}

error_reporting(E_ALL);

header('Cache-Control: no-store, no-cache, must-revalidate'); 
header('Cache-Control: post-check=0, pre-check=0', false); 

// This is the imposrtant part
require_once 'site.class.php';

// And create its object
$site = new Site();

?>
Now in every file/page called, which will be the base for doing that navigation bar, we will now make:
Code:
<?php
// Example: nokia.php file

define('IN_SITE', true);
require_once('common.php');
// Example
$site->SetPageTitle('Nokia 7260', 'nokia.php');

// ...
// Rest of the code here with files included or not and anywhere
// i mean first or after a file name or title calling.
// ...

print $site->GetPageTitle();
print $site->GetBasePage();

// Also a call or work with $site->GetBasePage(); in some included file will always produce/be 'nokia.php' as well.

?>
This way, everytime you call 'nokia.php' or other file coded the same way, the PAGE or FILE name and TITLE will always be the same doesn't matter how many files are included and if you're calling the page name or title in those included files.

-------

Well... not sure if this will be the same for you, 'cause i'm not sure about what you really want to do and of course, there's a bit to work around in this.

Anyways... hope it helps and sorry for this so big statement ;)
 
Thanks for that,

I will try and give that class or some similar workaround a try. I tried an echo from the functions file and it always returns the page that included it to begin with.

The only include I am using is of a functions file, but still no association with index.php which is what I don't understand. Why should script from index.php be executed when it is not being included?

Below is the script taken from above the initial <html> tag on the product_display.php.

Code:
<?php
session_start();
require_once("admin/includes/functionlib.inc.php");
dbconnect();

@$sql = "SELECT * FROM products WHERE prod_id = ".$_GET['id'];
$result = mysql_query($sql);

if (mysql_num_rows($result) != 1) {
	header("Location: index.php");	
} else {
	$row = mysql_fetch_assoc($result);
	$phone = $row['brand_name'] . " " . $row['name'];
}

//Add to views of phone
$sql = "UPDATE products SET views = views + 1 WHERE prod_id = ".$_GET['id'];
mysql_query($sql);
?>

Now if I kill the script after this extract and open the product display page, then comment it out and refresh the session vars are still intact. This would force me to believe there is something calling the index.php in the next part of script. Now I may have missed something that could be doing it from this code so maybe you could see something I have missed.

Code:
		  <?php
		/*
		if (!isset($_SESSION['something'])) {
		  	@$_SESSION['something'] = $_SESSION['page1'];
			@$_SESSION['dsagf'] = $_SESSION['page2'];
		}
		*/
		//print_r($_SESSION);
		  $lastpage = displayLocation($phone);
		  ?>
		<h1><?php echo $phone; ?></h1>
          <table width="100%"  border="0" cellspacing="12" cellpadding="0">
            <tr>
              <td width="200" valign="top"><div align="center"><a href="image_display.php?id=<?php echo $row['prod_id']; ?>"><img name="" src="" width="150" height="0" alt=""><br>
                <img src="<?php echo $config['imagedir']."medium/".$row['prod_id'].".jpg"; ?>" alt="<?php echo $phone; ?>" class="blackborder"><br>
                      <span class="small">Click to Enlarge</span> </a></div></td>
              <td width="100%" valign="top">
			  <p><?php echo $row['description']; ?></p>
                <p>
				<?php 
				if ($row['saleprice'] != 0 && $row['sale_expiry'] > time()) {
					//Display sale price followed by normal price
					$percent = round((($row['price1'] - $row['saleprice']) / $row['price1']) * 100);
					echo "<font color=\"red\"><b>Sale Price:</b></font><br>\n".
						 "<span class=\"saleprice\">$".$row['saleprice']."</span>\n".
						 "<br>Was $".$row['price1'].", reduced by ".$percent."%.\n".
						 "<br><span class=\"small\">Offer expires ".date("j F Y", $row['sale_expiry']).".</span>\n";
				} else {
					//Display normal price
					echo "<span class=\"price\">$".$row['price1']."</span>\n"; 
				}
				
				if ($row['price2'] != 0) {
					echo "<br><b>$".$row['price2']."</b> - ".$row['price2_desc']."\n";
				}
				?>
				</p>
               <a href="enquiry.php?id=<?php echo $row['prod_id']; ?>"><img src="images/enquire.gif" alt="Enquire about the <?php echo $phone; ?>" width="196" height="26" border="0"></a><br>
               <a href="<?php echo $lastpage; ?>"><img src="images/backtolastpage.gif" width="195" height="26" border="0"></a>               <br>
				<?php
				if ($row['features'] != '') {
					?>
				   <br>
					<table width="200" border="0" cellspacing="0" cellpadding="0">
					  <tr>
						<td colspan="2"><p><b>Features</b></p></td>
					  </tr>
					<?php
					$sql = "SELECT * FROM features";
					$result = mysql_query($sql);
					while ($featureRow = mysql_fetch_assoc($result)) {
						$feature[$featureRow['feature_id']] = $featureRow['name'];
					}
					
					$features = explode(' ', $row['features']);
					
					
					foreach ($features as $f) {
					  echo "
					  <tr>
						<td width=\"20\"><div align=\"right\"><img src=\"images/featurepoint.gif\"></div></td>
						<td width=\"180\"><p>".$feature[$f]."</p></td>
					  </tr>
					  ";
					}
					?>
					</table>
					<?php
				}
				?>
                <p class="small">All prices  in NZ$ including GST </p></td>
            </tr>
          </table>
 
Ok, anyway try to use this method in the files you're using to see what's the result by setting the PHP_SELF or BASE_PAGE and storing those values in your session variables.

Will be back after my todays' sleep :)
 
I still can't get over why this is even happening to begin with.

I have isolated the part on the product_display.php where index.php is happening.

Code:
<?php
<td width="200" valign="top"><div align="center">
<a href="image_display.php?id=<?php echo $row['prod_id']; ?>">
<?php  die; ?><img name="" src="" width="150" height="0" alt=""><?php die ?><br>
<img src="<?php echo $config['imagedir']."medium/".$row['prod_id'].".jpg"; ?>" alt="<?php echo $phone; ?>" class="blackborder">
<br>
?>
If the script is run like this then the correct location will continue to show and index.php will not be called at all. However, if the first die is commented out, then the session vars set by index.php are shown on refresh.

So this made me think, what happens between those two dies? Simple HTML. So I removed that simple HTML and it works perfectly. Part of me is relieved that it's working, but my curiousity is still stressing me out. However, if I put anything inside the src of that image, even if it's a non existent image (as I'm only using it to hold the cell width) then it also works fine.

So my conclusion is that Internet Explorer is trying to load the image "" which will default to index.php from Apache and then PHP will then parse the image "index.php" before it is sent to the web browser and therefore the scripts will be run and the session vars will be set.

I don't think I've been anymore satisfied.
 
ehhh... listen..when you call die() function: the script will stop from excecuting.. it will simly hault and will not continue working... why in the world are you using die when outputing html :S
 
The die is just there for testing purposes to find which part of the code is causing the problem. And it worked a treat.
 
Hunn... very, very weird :)
The die() is working, but maybe it's just stopping before the code which is causing the problem !?
Try using print_r($_SESSION); to check what's happening with your sessions variables.

Well... in last case you can use Apache to handle the "directories" part of the navigation bar.
 
Did you read my explanation of what was going wrong? I've figured it out now and it was nothing to do with PHP at all.

I've tried many print_r's all over the place and plenty of other attempts to narrow down the problem thats happening the die's were the main thing that helped me.

The die() was NOT being used in the code. The only reason I used it at all was simply to narrow down what was calling index.php.
 
Sorry if i've misunderstood.
Maybe you have to do what you want another way.
Like i said, you can use Apache (if you use it) to handle you navigation bar system.
 
OK, I have misunderstood you as well.

I'm quite happy to use my navigation bar system I've made myself, and the problem I had was the script in index.php was being executed somehow from product_display.php and hence making product_display.php think that the user came from index.php.

The problem was that I had this html - <img src="" width=150"> so apache was trying to serve the image "" and with index.php being the default page, this is how it was being called.
 
Back
Top