4 Replies - 3049 Views - Last Post: 25 July 2011 - 04:44 PM Rate Topic: -----

#1 dtvrij74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-July 11

PHP: Does not Destroy Session

Posted 24 July 2011 - 06:41 AM

Hello all.

I have to do a shopping cart project in PHP. The items will be in an XML file, and the shopping cart and product lists will all be in PHP. I have done all of that but I have a problem with the shopping cart. It seems that the session is not destroyed when I closed the browser and restart it. I can tell that by when I have 2(quantity) of item 1 in my cart. I close the browser and go back to my home page and add another item 1 to my cart. Now I have 3(quantity) of item 1. I have read somewhere that the session is automatically destroyed when the browser is closed, however, that is not the case for me. I have tried to put "session_destroy();" at the end of every file but that seems to not work. Here is my code for my product list page (intl.php):
<?php

session_start(); 


/*?>
<html>
<head><title>Shop News</title></head>
<body>
<?php*/
$mycart=$_SESSION['crt'];
if(empty($_SESSION['crt'])){
$mycart=array(1=>0,2=>0);
$_SESSION['crt'] = $mycart;
} else{


}

$xml = simplexml_load_file("dxml.xml");

echo $xml->getName() . "<br />";
$c=0;

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  $c++;
  echo "<a href=\"http://txdaniel.com/harvard/project/0/cart.php?id=" . $c . "\">ADD TO CART</a>";
  #echo " ";
}
 #php session_destroy();
?> 



I have an XML parser (for the file dxml.xml) and not much else. I click the hyperlink to +1 to my cart. I use a query string (?id=1) to help the cart identify what item to increase (in this case, the item with the id 1)

And here is my shopping cart code:
<?php session_start(); 



$mycart=$_SESSION['crt'];

if(empty($mycart)){
$mycart=array(1=>0,2=>0);
$_SESSION['crt'] = $mycart;
} else {


}


  $mid= $_GET['id'];


var_dump($_SESSION['crt']);
echo '<br />';

  if ($mid==1){
$_SESSION['crt'][1]++;
}


var_dump($_SESSION['crt']);

#session_destroy();

?> 
<a href="http://txdaniel.com/harvard/project/0/intl - Copy.php">SHOP MORE!</a>
</body>
</html>


All I have here is:
  • display cart before increase
  • if my cart is empty, I will create one
  • if the query string id (mid) == 1 then I will add 1 to item 1's quantity
  • display cart after increase
  • display "shop more" to lead back to home page


I don't know if it has something to do with my host (Godaddy.com) or there is an error. I think I'm missing something important here :stupid:

If you want to visit this project (work in progress) you can go here
. I will eventually fix my site to make it look "prettier".
Thanks for reading all of this. Please help me! :helpsmilie:

--dtvrij74

Is This A Good Question/Topic? 0
  • +

Replies To: PHP: Does not Destroy Session

#2 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: PHP: Does not Destroy Session

Posted 24 July 2011 - 07:11 AM

Just wondering is those # spose to be there?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: PHP: Does not Destroy Session

Posted 24 July 2011 - 07:17 AM

View Postdtvrij74, on 24 July 2011 - 09:41 AM, said:

I have read somewhere that the session is automatically destroyed when the browser is closed, however, that is not the case for me.

Sessions are not destroyed by closing the browser. The browser is a client application and PHP runs on the server, where it has no knowledge of browser actions unless the client sends that information, which you could do using Ajax on a browser close.
Was This Post Helpful? 1
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: PHP: Does not Destroy Session

Posted 24 July 2011 - 08:35 AM

View Postdtvrij74, on 24 July 2011 - 03:41 PM, said:

It seems that the session is not destroyed when I closed the browser and restart it.

when you close the browser, only the session cookie is deleted (given that you use the default settings). that means that you have no access to the session data any more (but the data are still kept until they are cleaned by the Session Garbage Collector)


How to destroy a session:

Quote

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

Was This Post Helpful? 1
  • +
  • -

#5 dtvrij74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-July 11

Re: PHP: Does not Destroy Session

Posted 25 July 2011 - 04:44 PM

Thanks for all the replies! I went and added
ini_set('session.use_only_cookies', 0);
to the top of all my .php files and it worked. Thank you all for the replies; it helped lead me to the right answer.

--dtvrij74
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1