Page 1 of 1
session_start() doesnt start
Posted: Sat Dec 27, 2008 11:51 am
by steven1
Hello,
Heres what im trying to do. I want a javascript function started onload once per session. below is the code im using and when going to the page nothing happens, no error message is displayed.
Code: Select all
session_start();
if (!isset($_SESSION['info'])) {
$_SESSION['info'] = true;
} else {
$_SESSION['info'] = false;
}
if ( $_SESSION['info'] ) {
if ( $info ) {
echo '<script language="javascript">';
echo '$(document).ready(function(){ ';
echo ' if (location.href.indexOf("/index.php") >0) { ';
echo ' tb_show("title", "/site/promo.php?TB_iframe=1&width=361&height=538", null); ';
echo ' }';
echo '}';
echo ');';
echo '</script>';
}
}
thanks for the help.
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 12:16 pm
by califdon
steven1 wrote:Hello,
Heres what im trying to do. I want a javascript function started onload once per session. below is the code im using and when going to the page nothing happens, no error message is displayed.
Code: Select all
session_start();
if (!isset($_SESSION['info'])) {
$_SESSION['info'] = true;
} else {
$_SESSION['info'] = false;
}
if ( $_SESSION['info'] ) {
if ( $info ) {
echo '<script language="javascript">';
echo '$(document).ready(function(){ ';
echo ' if (location.href.indexOf("/index.php") >0) { ';
echo ' tb_show("title", "/site/promo.php?TB_iframe=1&width=361&height=538", null); ';
echo ' }';
echo '}';
echo ');';
echo '</script>';
}
}
thanks for the help.
Doesn't look like you ever set the value of $info. It's not automatic. Somewhere you have to set $info=$_SESSION['info'];
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 12:42 pm
by steven1
Indeed, I didn't have that var set. Added it. Still doesn't resolve the issue. Still doesn't throw any error messages. thanks for the assistance though.
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 1:10 pm
by califdon
So what does happen? Is the javascript appearing in the source code on the first access? on subsequent accesses? That's what I think you are trying to achieve, isn't it? So is it working or not?
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 1:17 pm
by steven1
Sorry, I just checked the source and it doesnt show up anywhere. so perhaps more information will help. all the pages are called via index.php using the code below.
Code: Select all
<?
ini_set('display_errors', 1);
error_reporting(E_ALL);
//tb onload
$info = $_SESSION['info'];
session_start();
if (!isset($_SESSION['info'])) {
$_SESSION['info'] = true;
} else {
$_SESSION['info'] = false;
}
if ( $_SESSION['info'] ) {
if ( $info ) {
echo '<script language="javascript">';
echo '$(document).ready(function(){ ';
echo ' if (location.href.indexOf("/index.php") >0) { ';
echo ' tb_show("title", "/site/promo.php?TB_iframe=1&width=361&height=538", null); ';
echo ' }';
echo '}';
echo ');';
echo '</script>';
}
}
//Include nav config
include('/home/tandgweb/public_html/site/nav/config.php');
// if id is submitted in URL, $id is = this value $_GET['id']
// else $id = false
$id = isset($_GET['id']) ? $_GET['id'] : false;
if( $id === false ){
include('/home/tandgweb/public_html/site/home.php');
}
else
{
for($i=0; i<count($links); $i++)
{
if( $id == $links[$i] )
{
include( $url[$i] );
break;
}
}
}
?>
So now im thinking that this code doesnt work because of the way I have my "template" system in place. but I cant include it in the content page becuase I get the session already started ignoring session_start() error. I hope this info helps more. but again the code doesnt display at all.
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 1:22 pm
by califdon
You're trying to set the value of $info before you start the session.
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 1:27 pm
by steven1
Adjusted the var and placed it directly below session_start() and no go still. Page doesnt display it anywhere. I'm running out of ideas.
thanks for the help though.
edit: Quick question. Is it possiable that the issue is that index.php includes three html files (topper, content, footer) that all html is in these files. but I cant add this javascript to the topper in the head section becuase that returns that session was already started in index.php?
Re: session_start() doesnt start
Posted: Sat Dec 27, 2008 2:15 pm
by califdon
steven1 wrote:Adjusted the var and placed it directly below session_start() and no go still. Page doesnt display it anywhere. I'm running out of ideas.
thanks for the help though.
edit: Quick question. Is it possiable that the issue is that index.php includes three html files (topper, content, footer) that all html is in these files. but I cant add this javascript to the topper in the head section becuase that returns that session was already started in index.php?
Sounds like you need to diagram what code needs to go where. Don't just stick things in when you think of them. Plan the entire script. Includes merely save you the trouble of typing everything into one script file. When it is executed, wherever you place an Include or Required statement, that's where the code is copied into the combined output. So start with a diagram of what it would have to look like if it were all in one file, no includes. Then you should be able to see where session_start() and initialization of variables need to be.
Beyond that, be sure you understand what each step of your script should do. Then figure out how to determine for sure whether that step is happening. If it's not, that will lead you back to the code that should have made it happen. This is what you have to do with every script, every time you do any coding. For example, you've determined that the javascript code isn't getting sent to the browser, right? Why not? Well, look at the code to see why not. Your PHP code says that the javascript should be echoed to the browser
if the value of a variable $info is True (that is, not zero or null). So if it's not happening, it's sprobably because $info is NOT True. To check, you could echo the value of $info so you could confirm what value it is (echo "The value of info is: ".$info;). So why isn't it True? You have to go further back and look at where you set its value, right? That's the process you have to follow.