Asked  5 Months ago    Answers:  5   Viewed   49 times

For logging out a user from my website, I am redirecting the page to logout.php where I am using session_destroy() function. Even there also, logout functionality is not working without session_start() function. By adding session_start() function before session_destroy() function, I am able to logout the user successfully.

Why do I need to use session_start() function everytime and in every page where I am doing something related to sessions?

 Answers

87

session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.

Thursday, July 29, 2021
 
jab
answered 5 Months ago
jab
98

I noticed on Firefox with Firebug that your pages are all cached . Your session is working fine, but your page are cached, making login and logout quite (messed up).

Disable HTTP caching for your dynamic pages.

See Firebug output:

Response Headers

HTTP/1.1 304 Not Modified
Date: Thu, 14 Oct 2010 13:16:50 GMT
Server: Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Expires: Thu, 14 Oct 2010 16:16:50 GMT
Cache-Control: public, max-age=10800

Request Headers

GET / HTTP/1.1
Host: www.piataterenuri.info
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: PHPSESSID=55aea3f792334052dc673f85feb0b54a
If-Modified-Since: Wed, 13 Oct 2010 13:47:53 GMT
Cache-Control: max-age=0

PHP manual has already an example on how to disable caching:

http://php.net/manual/en/function.header.php

<?php

header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

?>
Wednesday, March 31, 2021
 
AntoineB
answered 9 Months ago
20

This is how I finally solved it (if a better implementation arises, I will for sure recode). It is an implementation of solution under the accepted answer here: Global or Singleton for database connection?

My ConnFactory.php

include('config/config.php');

class ConnFactory
{
    private static $factory;

    public static function getFactory()
    {
        if(!self::$factory){
            self::$factory = new ConnFactory();
            return self::$factory;
        }

    }

    private $db;

public function pdo()
{
    if(!$this->db){
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
        );
        $this->db = new PDO("mysql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_SCHEMA."", DB_USER, DB_PASS, $options);
    }
    return $this->db;
    }

}

Usage in my view/html file (just a test of insert functionalty):

$entry = new Entry();
$entry->name = "Kartonaža ad Grada?ac";
$entry->seoName = "kartonaza-ad-gradacac";
$entry->timeCreated = date("Y-m-d H:i:s");

$entryMapper = new EntryMapper(ConnFactory::getFactory()->pdo());
$entryMapper->saveEntry($entry);
Wednesday, March 31, 2021
 
mertak
answered 9 Months ago
82

http://nl2.php.net/manual/en/function.session-destroy.php

Take a look at example 1 here. It clearly states that you have to clear $_SESSION as well.

if(isset($_GET['logout'])) {
    unset($_SESSION['u_name']); //makes it non-existent (it does unset) that variable
    session_destroy();
    header("Location:emprego.php");
}
Saturday, May 29, 2021
 
EurekA
answered 7 Months ago
32

I would write an add to basket function like this:

function AddToBasket(){
    if(is_numeric($_GET["ID"])){
        $ProductID=(int)$_GET["ID"];
        $_SESSION["Basket"][]=$ProductID;
        $sOut.=ShowBasketDetail();
        return $sOut; 
    }
}

In this shopping basket function we save Product IDs in an session array.

Here is what I would have in the show basket function:

function ShowBasket(){
    foreach($_SESSION[Basket] as $ProductID){
        $sql="select * from products where ProductID=$ProductID";
        $result=mysql_query($sql);
        $row=mysql_fetch_row($result);
        echo "Product: ".$row[0];
        }
}

For each ProudctID in our session basket we make a SQL query to output product information.

Now last but not least, a clear basket function:

function ClearBasket(){
    unset($_SESSION[Basket]);
}

Don't forget session_start(); before you add any Product IDs to your session basket. Also don't forget the mysql_connect(); function, you need this before you make any queries with the database.

Saturday, May 29, 2021
 
BlueNile
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share