Page 1 of 2

Passing sessions problem [SOLVED]

Posted: Mon Sep 17, 2007 2:05 am
by lyleyboy
Hi, I am building a site that uses one navigation pane that I include on each page. I now need to pass a url to this page.

I have tried to pass this url via sessions and just via a variable.
If I browse to the nav page the session works ok but when it is included into the rest of the script (where the session is created) nothing displays.

HELP!

Posted: Mon Sep 17, 2007 4:14 am
by CoderGoblin
Can you give a sample of your code..

My Code

Posted: Mon Sep 17, 2007 4:50 am
by lyleyboy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is the main page.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css_style.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColElsLt #sclassebar1 { padding-top: 30px; }
.twoColElsLt #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoclass several bugs */
</style>
<![endif]-->
<title>Flittermouse.com Web based programming</title>
</head>

<body>

<div class="container">
  <div class="left">

		
<?php
//Include the navigation pane.
include('includes/nav_mail.php');

//Page content goes between here
echo "<br/><br/><span class=\"blue_title\">What were working on</span><br/><br/>";
$File = "includes/site_news.txt";
	$fh = fopen($File, 'r');
	$Data = fread($fh, 150);
	fclose($fh);
	echo $Data . "...";
?>
			
  <!--End of page Content-->
  <!-- end #left --></div>
  <div class="right">
    <div class="title"><img src="images/title.png"></div>
		<br />
        
<!--This is the start of the right hand pane-->
		<img src="images/about_fm.png">
		<br />
			
<?php

include('../includes/config.php');

?>
<span class="blue_title"><a href="free/">Free Webscripts and Snippets</a></span><br/>
<?

$query = "SELECT * FROM script_list WHERE price='free' and active='on' ORDER BY NAME"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
	
	echo "<span class=\"plain_text\">";

	echo "<a href=\"" . $row['url'] . "\">" . $row['name'] . " (Version " . $row['version'] . ") <span class=\"red\">Free</span></a><br/>";
	
	echo "</span>";

}

?><br/>
<span class="blue_title"><a href="premium/">Premuim Webscripts</a></span><br/>
<?

$query = "SELECT * FROM script_list WHERE price!='free' and active='on' ORDER BY NAME"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
	
	echo "<span class=\"plain_text\">";
	echo "<a href=\"" . $row['url'] . "\">" . $row['name'] . " (Version " . $row['version'] . ") </a><br/>";
	//echo $row['name'] . " (Version " . $row['version'] . ") <br/>";

	echo "</span>";
}

?>
			
	<!-- end of right right hand pane--></div>
	<br class="clearfloat" />
<!-- end #container --></div>
</body>
</html>
This is the include.

Code: Select all

<?php
echo $_SERVER['PHP_SELF'];
$url = $_SESSION['url'];
echo $url;
echo $_SESSION['url'];
?>
<img src="images/nav_bg_top.png"><br/>
		<div class="nav">
			
<div><a href="<?php echo $url; ?>/index.php"><img src="images/nav_home.png" alt="Home Link" border="0">SCRIPT HOME</a><br/></div>

<div><a href="<?php echo $url; ?>/sys_req.php"><img src="images/settings.png" alt="System Requirements" border="0">SYSTEM REQUIREMENTS</a><br/></div>

<div><a href="<?php echo $url; ?>/demo"><img src="images/nav_demo.png" alt="Demo" border="0">DEMO</a><br/></div>

<div><a href="<?php echo $url; ?>/rel_notes.php"><img src="images/nav_releasenotes.png" alt="Release Notes" border="0">RELEASE NOTES</a><br/></div>          

<div><a href="<?php echo $url; ?>/download"><img src="images/snav_download.png" alt="Search Scripts" border="0">DOWNLOAD</a><br/></div>

<div><a href="scripts/free"><img src="images/nav_left.png" alt="Back to free scripts" border="0">BACK TO FREE SCRIPTS</a><br/></div>
		
		</div><!--nav-->
		<img src="images/nav_bg_bottom.png">
The first few lines of the include are my desperate attemptes to get this to work.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Sep 17, 2007 4:56 am
by CoderGoblin
Where do you have session_start() ? Note that this command must be used before any output to avoid a headers already sent error.

Session

Posted: Mon Sep 17, 2007 6:01 am
by lyleyboy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


The session_start() is the first line. I magically didnt copy that part but it is there. It also only occurs once in the index.php I have not put it into the included nav_free.php file as this is then included into the index.

Bit of background to hopefully make things clearer. The index and nav_free are not in the same folder.

Code: Select all

<?php
session_start();
include('../../../includes/config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="http://www.flittermouse.com/scripts/css_style.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColElsLt #sclassebar1 { padding-top: 30px; }
.twoColElsLt #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoclass several bugs */
</style>
<![endif]-->
<title>Flittermouse.com Web based programming</title>
</head>

<body>

<div class="container">
  <div class="left">

		
<?php
//Setup the navigation
$File = "inc_id.txt";
	$fh = fopen($File, 'r');
	$file_id = fread($fh, 100);
	fclose($fh);
	echo $file_id;
$query = "SELECT * FROM script_list WHERE id='$file_id'"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

	$url = $row['url'];
	}
	$_SESSION['url'] = $url;
	echo $_SESSION['url'];
	echo $url;
//Include the navigation pane.
//include('http://www.flittermouse.com/includes/nav_free.php');
$File = "http://www.flittermouse.com/includes/nav_free.php";
	$fh = fopen($File, 'r');
	$nav = fread($fh, 10000);
	fclose($fh);
	echo $nav;

//Page content goes between here
//Read ID from file
$File = "inc_id.txt";
	$fh = fopen($File, 'r');
	$file_id = fread($fh, 100);
	fclose($fh);
$query = "SELECT * FROM script_list WHERE id='$file_id'"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
	
	$xhtml = $row['xhtml'];
	$java =  $row['java'];
	$php = $row['php'];
	$asp = $row['asp'];
	$cgi = $row['cgi'];
	$css = $row['css'];
	}
echo "<span class=\"blue_title\">Languages used</span><br/>";
	if($xhtml=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/xhtml.png\" alt=\"XHTML\">";
	$xhtml_disp = "XHTML";
	}
	if ($java=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/js.png\" alt=\"Javascript\">";
	$java_disp = "Javascript";
	}
	if ($php=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/php.png\" alt=\"PHP\">";
	$php_disp = "PHP";
	}
	if ($asp=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/asp.png\" alt=\"ASP\">";
	$asp_disp="ASP";
	}
	if ($cgi=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/cgi.png\" alt=\"CGI\">";
	$cgi_disp="CGI";
	}
	if ($css=="on"){
	echo "<img src=\"http://www.flittermouse.com/images/css.png\" alt=\"CSS\">";
	$css_disp="CSS";
	}
	echo "<br/>This script was written using the following coding languages:-<br/>";
	echo $xhtml_disp . "<br/>";
	echo $java_disp . "<br/>";
	echo $php_disp . "<br/>";
	echo $asp_disp . "<br/>";
	echo $cgi_disp . "<br/>";
	echo $css_disp . "<br/>";
?>
			
  <!--End of page Content-->
  <!-- end #left --></div>
  <div class="right">
    <div class="title"><img src="http://www.flittermouse.com/images/title.png"></div>
		<br /><br /><br /><br />
        
<!--This is the start of the right hand pane-->
				
<?php
//Read ID from file
$File = "inc_id.txt";
	$fh = fopen($File, 'r');
	$file_id = fread($fh, 100);
	fclose($fh);
	
$query = "SELECT * FROM script_list WHERE id='$file_id' and active='on'"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
	
	echo "<span class=\"blue_title\">" . $row['name'] . "</span><br/><br/>";
	echo $row['description'];
	}


?>
<br/>

			
	<!-- end of right right hand pane--></div>
	<br class="clearfloat" />
<!-- end #container --></div>
</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Session

Posted: Mon Sep 17, 2007 6:09 am
by volka
lyleyboy wrote:I magically didnt copy that part but it is there.
So this is the "real" code that replaces the first version (suggested working title:"fairy tale" )? ;)

Correct

Posted: Mon Sep 17, 2007 6:21 am
by lyleyboy
Not sure why it didnt copy cos it did the second time. Who knows?

Posted: Mon Sep 17, 2007 6:28 am
by volka
please replace
<?php
session_start();
include('../../../includes/config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
by

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('mysql.trace_mode', true);

session_start();
include('../../../includes/config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
and
$query = "SELECT * FROM script_list WHERE id='$file_id'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

$url = $row['url'];
}
$_SESSION['url'] = $url;
echo $_SESSION['url'];
echo $url;
//Include the navigation pane.
by

Code: Select all

$query = "SELECT * FROM script_list WHERE id='$file_id'";
echo '<div>Debug query: "', htmlentities($query), "\"</div>\n";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
  $url = $row['url'];
  echo '<div>Debug url: "', htmlentities($url), "\"</div>\n";
}
$_SESSION['url'] = $url;
echo $_SESSION['url'];

//Include the navigation pane.
and try again

Nice pretty error message

Posted: Mon Sep 17, 2007 6:34 am
by lyleyboy
Debug query: "SELECT * FROM script_list WHERE id='4'"
Debug url: "http://www.flittermouse.com/scripts/free/javadd"
http://www.flittermouse.com/scripts/fre ... v_free.php
Home LinkSCRIPT HOME


Notice: Undefined variable: php_disp in /homepages/8/d92992469/htdocs/Flittermouse/scripts/free/javadd/index.php on line 100


Notice: Undefined variable: asp_disp in /homepages/8/d92992469/htdocs/Flittermouse/scripts/free/javadd/index.php on line 101


Notice: Undefined variable: cgi_disp in /homepages/8/d92992469/htdocs/Flittermouse/scripts/free/javadd/index.php on line 102


Notice: Undefined variable: css_disp in /homepages/8/d92992469/htdocs/Flittermouse/scripts/free/javadd/index.php on line 103



Warning: (null)() [function.mysql-free-result]: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0

Re: Nice pretty error message

Posted: Mon Sep 17, 2007 6:49 am
by volka
:?:
echo $_SESSION['url']; prints something else than echo '<div>Debug url: "', htmlentities($url), ""</div>\n";?

For clarity's sake please try again with

Code: Select all

echo 'session: "', $_SESSION['url'], '" ';

//Include the navigation pane.

I hope you know what this means

Posted: Mon Sep 17, 2007 6:53 am
by lyleyboy
Debug query: "SELECT * FROM script_list WHERE id='4'"
Debug url: "http://www.flittermouse.com/scripts/free/javadd"
session: "http://www.flittermouse.com/scripts/free/javadd" /includes/nav_free.php

Warning: (null)() [function.mysql-free-result]: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0

These two overlap on the screen due to the css so they might be in the wrong order.

Posted: Mon Sep 17, 2007 7:01 am
by volka
Ok, now you have $_SESSION['url'].
Where do you want to use this variable and where is it not present?

The other page (Include)

Posted: Mon Sep 17, 2007 7:06 am
by lyleyboy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is supposed to show in the other page. The Include.

Each Div below is simply an icon and text link for the nav. It should set up the front part of the url.

so:-

Code: Select all

<a href="<?php echo $url; ?>/index.php">
should read

Code: Select all

<a href="http://www.url.com/index.php">

Code: Select all

<?php
echo $_SERVER['PHP_SELF'];
$url = $_SESSION['url'];
echo $url;
echo $_SESSION['url'];
?>
<img src="http://www.flittermouse.com/images/nav_bg_top.png"><br/>
		<div class="nav">
			
<div><a href="<?php echo $url; ?>/index.php"><img src="http://www.flittermouse.com/images/nav_home.png" alt="Home Link" border="0">SCRIPT HOME</a><br/></div>

<div><a href="<?php echo $url; ?>/sys_req.php"><img src="http://www.flittermouse.com/images/settings.png" alt="System Requirements" border="0">SYSTEM REQUIREMENTS</a><br/></div>

<div><a href="<?php echo $url; ?>/demo"><img src="http://www.flittermouse.com/images/nav_demo.png" alt="Demo" border="0">DEMO</a><br/></div>

<div><a href="<?php echo $url; ?>/rel_notes.php"><img src="http://www.flittermouse.com/images/nav_releasenotes.png" alt="Release Notes" border="0">RELEASE NOTES</a><br/></div>          

<div><a href="<?php echo $url; ?>/download"><img src="http://www.flittermouse.com/images/snav_download.png" alt="Search Scripts" border="0">DOWNLOAD</a><br/></div>

<div><a href="http://www.flittermouse.com/scripts/free"><img src="http://www.flittermouse.com/images/nav_left.png" alt="Back to free scripts" border="0">BACK TO FREE SCRIPTS</a><br/></div>
		
		</div><!--nav-->
		<img src="http://www.flittermouse.com/images/nav_bg_bottom.png">

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Sep 17, 2007 7:11 am
by volka
Next debug output session ;)
please replace
<?php
echo $_SERVER['PHP_SELF'];
$url = $_SESSION['url'];
echo $url;
echo $_SESSION['url'];
?>
by

Code: Select all

<?php
echo '<pre>Debug call stack: ', print_r(debug_backtrace()), "</pre>\n";
echo '<pre>Debug _SESSION: ', print_r($_SESSION), "</pre>\n";
echo $_SERVER['PHP_SELF'];
$url = $_SESSION['url'];
?>

Could be in the wrong order

Posted: Mon Sep 17, 2007 7:16 am
by lyleyboy
Debug query: "SELECT * FROM script_list WHERE id='4'"
Debug url: "http://www.flittermouse.com/scripts/free/javadd"
session: "http://www.flittermouse.com/scripts/free/javadd"

Debug call stack: Array
(
)
1

Debug _SESSION: 1

/includes/nav_free.php



Warning: (null)() [function.mysql-free-result]: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0