Page 1 of 1

problem with session_start()

Posted: Sun Jun 11, 2006 6:30 pm
by jonwondering
I have the following piece of code:

Code: Select all

<?php

	session_start();

	function Login($username, $password)
	{

		// If user was in the middle of filling out new account form,
		// and decided to login, then clear all the junk the user filled out.
		if ($_SESSION['signup_account']) {
			session_destroy(); // line 11
			session_start();
		}

	}

?>
and I got this error today from the error logs:

Code: Select all

PHP Warning:  session_destroy() [<a href='function.session-destroy'>function.session-destroy</a>]: Trying to destroy uninitialized session in /home/username/public_html/checklogin.php on line 11
doesn't it count as session_start() when it's outside of the function? or am i missing something else here?

Posted: Sun Jun 11, 2006 6:53 pm
by derchris
and what about this:

Code: Select all

<?php 

        session_start(); 

        function Login($username, $password) 
        { 

                // If user was in the middle of filling out new account form, 
                // and decided to login, then clear all the junk the user filled out. 
                if ($_SESSION['signup_account']) { 
                   session_start();     
                   session_destroy(); // line 11 
                         
                } 

        } 

?>

Posted: Sun Jun 11, 2006 6:55 pm
by jonwondering
yeah, i guess i'll have to try that. unfortunately, its one of those errors that happen only half the time.... so i can't really track it down. but i will try that, thanks tho.

Posted: Sun Jun 11, 2006 7:16 pm
by John Cartwright
derchris wrote:and what about this:

Code: Select all

<?php 

        session_start(); 

        function Login($username, $password) 
        { 

                // If user was in the middle of filling out new account form, 
                // and decided to login, then clear all the junk the user filled out. 
                if ($_SESSION['signup_account']) { 
                   session_start();     
                   session_destroy(); // line 11 
                         
                } 

        } 

?>
you'll run into an session already started error on that one.. I personally just do $_SESSION = array(); or alike when wanting to destroy the user's data.

Posted: Sun Jun 11, 2006 7:16 pm
by John Cartwright
derchris wrote:and what about this:

Code: Select all

<?php 

        session_start(); 

        function Login($username, $password) 
        { 

                // If user was in the middle of filling out new account form, 
                // and decided to login, then clear all the junk the user filled out. 
                if ($_SESSION['signup_account']) { 
                   session_start();     
                   session_destroy(); // line 11 
                         
                } 

        } 

?>
you'll run into an session already started error as well as some indefined index notice (sometimes) on that one.. I personally just do $_SESSION = array(); or alike when wanting to destroy the user's data.

Posted: Sun Jun 11, 2006 7:37 pm
by jonwondering
yeah! i haven't though about it - that's pretty cool, just clearing the whole $_SESSION array on that one... but why does it not recognize the session_start() ?

Posted: Mon Jun 12, 2006 6:04 am
by technofreak
Buddy,

You have just started the sesson with session_start(); at the top of the code... that makes the $_SESSION global variable array available to the particular php page. But, you need to initialize something as the session variable before you call an session-destory() function.

For example..

Code: Select all

<?php 
session_start();
?>



<?php 
$_SESSION['name'] = $my_name;
?>

<?php
session_destroy();
?>
Other than the session_start() function, which needs to be declared before the body of the page is sent, other can be placed anywhere in the code.

It is better to use a unser() function, along (and after) with the session_destroy(), to unset the initialized variables.

Posted: Mon Jun 12, 2006 11:54 am
by jonwondering
but I already had session variables stored... if i didn't, it would not have recognized $_SESSION['signup_account'], and would not have called the destroy function. and 'signup_account' is not the only session variable in the session, there are others from other php files.