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?



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

Thursday, July 29, 2021
answered 5 Months ago

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/
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
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv: 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:


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
answered 9 Months ago

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


class ConnFactory
    private static $factory;

    public static function getFactory()
            self::$factory = new ConnFactory();
            return self::$factory;


    private $db;

public function pdo()
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_EMULATE_PREPARES => false,
        $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());
Wednesday, March 31, 2021
answered 9 Months ago

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
Saturday, May 29, 2021
answered 7 Months ago

I would write an add to basket function like this:

function AddToBasket(){
        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";
        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(){

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
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 :