hi,

i'm connecting to my database and placing the connection in a variable like so

<?php

$conn = mysql_connect("localhost", "*", "*");
$db = mysql_select_db("theuther_content", $conn);

$_SESSION['conn'] = $conn;

?>

and calling to it as normal.

However on one of my pages i am using session_unset(); to help a logout process...

<?
session_unset();
unset($_SESSION['blogger']);
unset($_SESSION['auth']);

if (empty($_SESSION['blogger'])) {
	echo "Logged Out";
}
else {
	echo "NOT Logged Out";
}
?>

and then when i call to $conn later in my page i get an error saying that the link identifier is invalid.

I assume that when $_SESSION['conn'] is unset the contents of it are unset (the $conn variable), but why does this happen?

    are you calling to $conn later on the same page where it was created?
    i have tried replicating this problem on 2 different servers but my $conn variable still lives even after unsetting the session and destroying the variable.

    p.s. as per the php manual, you should not use session_unset() if you are using $SESSION or $HTTP_SESSION_VARS.

    also the line
    if (empty($
    SESSION['blogger'])) {
    would work better if it was
    if (!isset($_SESSION['blogger'])) {
    since you destroyed the variable 3 lines up.

      yeh i am calling to it through a normal query

      $qry = mysql_query($sql, $conn) or die(mysql_error());

      as for the session_unset i got rid of that so my problem has gone

      <? 
      
      $_SESSION['blogger'] = "";
      $_SESSION['auth'] = "";
      
      unset($_SESSION['blogger']);
      unset($_SESSION['auth']);
      
      if (!isset($_SESSION['blogger'])) {
      	echo "Logged Out";
      }
      
      else {
      	echo "NOT Logged Out";
      }
      
      ?>
      

      but why did $conn not work after i used session_unset ??

        if you are familiar with c++ and pointers, my original thought was that since $conn was a resource variable $SESSION['conn'] was a pointer to $conn so when you unset the session variable, it unset the $conn variable too. but that is what i tried testing on my own machines and it wasnt the case. somehow the two must have been linked together by doing $SESSION['conn'] = $conn;

        in c++ i thought it would be equivalent to
        _SESSION['conn'] = &conn;
        so the session var points to conn and any changes to the session var really change the conn var so closing the connection of the session var would close conn too.

          Write a Reply...