problem with session_start()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

problem with session_start()

Post 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?
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post 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 
                         
                } 

        } 

?>
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post 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() ?
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post 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.
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Post 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.
Post Reply