User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 3 Mar 2003, 17:51   #1
Cicada
p a r r a c i d a
 
Join Date: Apr 2000
Location: #titans
Posts: 511
Cicada is an unknown quantity at this point
function / session issue (php)

i have a small problem here, just wondering if anybody knows a solution..

i have an issue with a function registering a session, ie, when it exists the function, the session is registered but the variable registered is empty..

the function is used to work out the currrent time the session is open. It's in a seprate file called "functions.php":

Code:
function session_information() {
	//session info
	if (session_is_registered("stamp"))	{
		
		$current_stamp=mktime(date(H),date(i),date(s),date(n),date(d),date(Y));
		
		//get session activity time (24 hours only)
		$added_minutes=0;$added_hours=0;
		$active_seconds=date("s",$current_stamp)-date("s",$stamp);
		if ($active_seconds<0) {
			$active_seconds=$active_seconds+60;
			$added_minutes=1;
		}
		$active_minutes=date("i",$current_stamp)-date("i",$stamp);
		$active_minutes=$active_minutes-$added_minutes;
		if ($active_minutes<0) {
			$active_minutes=$active_minutes+60;
			$added_hours=1;
		}
		$active_hours=date("H",$current_stamp)-date("H",$stamp);
		$active_hours=$active_hours-$added_hours;;
		if ($active_hours<0) {
			$active_hours=$active_hours+24;
		}
	}
	else	{
		//add ind hits
		//$Query = "UPDATE hits set ind_hits='$ind_hits' where y_m='$Date'";
		//mysql_query($Query);
		
		//add new session ip thingy
		$Day=date("d");
		$Month=date("m");
		$Year=date("Y");
		$Date=$Year."-".$Month."-".$Day;
		
		$Hour=date("H");
		$Min=date("i");
		$Sec=date("s");
		$Time=$Hour.":".$Min.":".$Sec;
		
		//get ip
		$ip = getenv ("REMOTE_ADDR");
		
		//$Query = "INSERT into ip values ('0','$ip','$Date','$Time')";
		//mysql_query($Query);
		
		//time stamp
		$stamp=mktime(date(H),date(i),date(s),date(n),date(d),date(Y));
		$active_seconds=0;
		$active_minutes=0;
		$active_hours=0;
		
		session_register("stamp");
		echo date("H",$stamp);
	}
	?>Session Time : <strong>[ </strong><? echo $active_hours."h<strong>:</strong>".$active_minutes."m<strong>:</strong>".$active_seconds; ?>s<strong> ]</strong>
<?}
the page which loads the function is as follows:

Code:
include ("link.php");
include ("functions.php");

session_start();

/* split up page into array */
$query = "select * from template where page='index'";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))	{ 
	$frag = explode("\n",$row[html]);
}

/* run through array breakdown */
for ($i=0;$i<count($frag);$i++) {
	if (strstr($frag[$i],"<!--NEWS-->")) { 
		/* code to enter if the news is requested by the html */
		$items = array("date","news");
		get_data(row,right,news,$items,"order by date desc, id desc limit 5");
	}
	elseif (strstr($frag[$i],"<!--POLL-->")) {
		/* code to enter if a poll is requested by the html */
		random_poll();
	}
	elseif (strstr($frag[$i],"<!--IMAGE-->")) {
		/* code to enter if an image is requested by the html */
		?> <img src="images/temp.jpg" width="350"> <?
	}
	elseif (strstr($frag[$i],"<!--SESSION-->")) {
		/* code to enter if session information is requested by the html */
		session_information();
		echo date("H",$stamp);
	}
	else { 
		/* display html */
		echo $frag[$i]." "; 
	}
}
?>
now, if i put the function code into where i call it (session_information), the function code works fine, but it's not working in the function. This code does need to be duplicated numerous times, so it needs to be in the function..

any ideas cause it's god me stumped?
__________________
Cicada || No Warning, No Mercy, No Ambiguity || [Titans] [F.E.A.R]
Cicada is offline   Reply With Quote
Unread 3 Mar 2003, 23:07   #2
Add
Registered User
 
Join Date: Feb 2001
Posts: 442
Add will become famous soon enoughAdd will become famous soon enough
i cannot for the life of me work out what you are trying to do here, or why you are trying to do it. It just doesn't make any sence. However, this should fix it:

PHP Code:
<?php
function session_information() {
    global 
$stamp$active_minutes$active_seconds$active_hours;
    
//session info
    
if (session_is_registered('stamp'))    {
        
$current_stamp time(); //returns current unix timestamp
        
        //get session activity time (24 hours only)
        
$added_minutes 0;
        
$added_hours 0;
        
$active_seconds date('s',$current_stamp) - date('s',$stamp);
        if (
$active_seconds 0) {
            
$active_seconds $active_seconds 60;
            
$added_minutes 1;
        }
        
$active_minutes date('i',$current_stamp) - date('i',$stamp);
        
$active_minutes $active_minutes $added_minutes;
        if (
$active_minutes 0) {
            
$active_minutes $active_minutes 60;
            
$added_hours 1;
        }
        
$active_hours date('H',$current_stamp) - date('H',$stamp);
        
$active_hours $active_hours $added_hours;
        if (
$active_hours 0) {
            
$active_hours $active_hours 24;
        }
    }
    else    {
        
//add ind hits
        //$Query = "UPDATE hits set ind_hits='$ind_hits' where y_m='$Date'";
        //mysql_query($Query);
        
        //add new session ip thingy
        
$Date date('d-m-Y');
        
$Time=date('H:i:s')
        
        
//get ip
        
$ip getenv('REMOTE_ADDR');
        
        
//$Query = "INSERT into ip values ('0','$ip','$Date','$Time')";
        //mysql_query($Query);
        
        //time stamp
        
$stamp=time();
        
$active_seconds=0;
        
$active_minutes=0;
        
$active_hours=0;
        
        
session_register("stamp");
        echo 
date("H",$stamp);
    }
?>
Session Time : <strong>[ </strong><? echo $active_hours."h<strong>:</strong>".$active_minutes."m<strong>:</strong>".$active_seconds?>s<strong> ]</strong>
<?php
}
?>


<?php
session_start
();

include (
"link.php");
include (
"functions.php");


/* split up page into array */
$query "select * from template where page='index'";
$result=mysql_query($query);
while(
$row=mysql_fetch_array($result))    { 
    
$frag explode("\n",$row[html]);
}

/* run through array breakdown */
for ($i=0;$i<count($frag);$i++) {
    if (
strstr($frag[$i],"<!--NEWS-->")) { 
        
/* code to enter if the news is requested by the html */
        
$items = array("date","news");
        
get_data(row,right,news,$items,"order by date desc, id desc limit 5");
    }
    elseif (
strstr($frag[$i],"<!--POLL-->")) {
        
/* code to enter if a poll is requested by the html */
        
random_poll();
    }
    elseif (
strstr($frag[$i],"<!--IMAGE-->")) {
        
/* code to enter if an image is requested by the html */
        
?> <img src="images/temp.jpg" width="350"> <?
    
}
    elseif (
strstr($frag[$i],"<!--SESSION-->")) {
        
/* code to enter if session information is requested by the html */
        
session_information();
        echo 
date("H",$stamp);
    }
    else { 
        
/* display html */
        
echo $frag[$i]." "
    }
}
?>
__________________
Trust in my Instinct
Add is offline   Reply With Quote
Unread 4 Mar 2003, 04:58   #3
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
session_start(); before you include the function :)
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 4 Mar 2003, 12:39   #4
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by MT
session_start(); before you include the function :)
MT is clueless.

Local function variables aren't available in global scope - use ``global $stamp''. RTFM.

Edit: you also want to use $_SESSION["stamp"] in the main bit and in the first branch of your session_information function instead of $stamp. If register_globals is off which it should be. stamp is a session variable after all.

Last edited by queball; 4 Mar 2003 at 12:49.
queball is offline   Reply With Quote
Unread 4 Mar 2003, 15:23   #5
grkn
Shadow of the past
 
Join Date: Apr 2001
Posts: 39
grkn is an unknown quantity at this point
It should be noted that the usage of session_* functions should be discarded, as this in combination with register_globals = off and a PHP version prior to 4.3.x will result in a nonoperational script.

Instead use the predefined global array $_SESSION[1].

session_register("blah") -> $_SESSION['blah']
session_is_registered("blah") -> isset($_SESSIN['blah])
...

In general it is advised to use - for the sake of portability - $_SESSION regardless of your php.ini config.

[1] Note: In old PHP versions this superglobal was named $HTTP_SESSION_VARS, which is now deprecated. If you're still using such an old fish, you can work arround it by adding a $_SESSION = &$HTTP_SESSION_VARS; at the start of the script.
__________________
karrde, Retired head of "Myrkrs roid liberation and smuggling Inc."

One OS to rule them all, One Passport to find them,
One OS to bring them all, And with the EULA bind them.
In the land of Redmond where the windows lie.


"...to leave the Elysium the dead had to drink from and travel beyond the River of Oblivion, Lethe, to once again return to the mortal realm..."
grkn is offline   Reply With Quote
Unread 4 Mar 2003, 17:09   #6
Cicada
p a r r a c i d a
 
Join Date: Apr 2000
Location: #titans
Posts: 511
Cicada is an unknown quantity at this point
Quote:
Originally posted by queball
MT is clueless.

Local function variables aren't available in global scope - use ``global $stamp''. RTFM.

Edit: you also want to use $_SESSION["stamp"] in the main bit and in the first branch of your session_information function instead of $stamp. If register_globals is off which it should be. stamp is a session variable after all.
thanks..this was the solution i needed
__________________
Cicada || No Warning, No Mercy, No Ambiguity || [Titans] [F.E.A.R]
Cicada is offline   Reply With Quote
Unread 4 Mar 2003, 17:40   #7
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
It's still good practice to put session_start(); at the very top of the very first page.

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 4 Mar 2003, 18:07   #8
Add
Registered User
 
Join Date: Feb 2001
Posts: 442
Add will become famous soon enoughAdd will become famous soon enough
Quote:
Originally posted by Cicada
thanks..this was the solution i needed
which is what i did in my code in the FIRST REPLY ON THIS THREAD

:\
__________________
Trust in my Instinct
Add is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 14:24.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018