Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to declare multiple sessions inside php(within if )?

Status
Not open for further replies.

Edward07

Programmer
Apr 13, 2007
49
NL
i wonder if i can declare a new session every time part my php code runs. For example i have one decleration of session at start of php and i want to declare a new session with else if statment and pass its key.

Right now when i call :
Code:
./script.php?cmd=file&file=visitorWantsToChat
the first sessionkey is passed to:
Code:
./script.php?cmd=file&file=chatFrame

What i want each time this script called:
Code:
./script.php?cmd=file&file=visitorWantsToChat
new session key is passed to :
Code:
./script.php?cmd=file&file=chatFrame
So my main goals is to get new sessin key for the
./script.php?cmd=file&file=chatFrame without user have to
close browser .I be happy if some one show me how this can be done.Thanks

Note: i don't want to kill the first sessionkey since i need that sessionkey to avoid duplication in db

script.php
Code:
<?

session_start();

$engage="./script.php?cmd=file&file=chatFrame&sessionkey=" . session_id() . "";

if($cmd=="inPage" && $visitorStatus=="INSITE_STATUS")
{
writetodb();
}
else if ($cmd=="file" && $file=="visitorWantsToChat")
{
  header("Location: $engage");
}
[b]else if ($cmd=="file" && $file=="chatFrame")
{
/// i want to get new session each time this part runs
}[/b]
else
{

}

function writetodb()
{

$server   = "localhost"; // MySQL hostname
$username = "root"; // MySQL username
$password = ""; // MySQL password
$dbname   = "db"; // MySQL db name

$db = mysql_connect($server, $username, $password) or die(mysql_error());
      mysql_select_db($dbname) or die(mysql_error());

    $query = "SELECT * FROM visitor WHERE who_sessid = \"" . session_id() . "\" ";
    $result = mysql_query( $query );

   if( mysql_num_rows( $result ) ) 
    {
     //update visitor table
   }
   else
   {
$query2 = "INSERT INTO visitor (`ID`, `ip`, `date`,`who_sessid`,) VALUES ('$ID','".$_SERVER['REMOTE_ADDR']."',NOW(),\"" . session_id() . "\")";
        
 $result2 = mysql_query( $query2 );
   }

}


 
Why on earth would you want multiple session IDs for a single user? That is in direct antithesis to the way PHP sessions (and, indeed, the session-handling mechanism of every web programming language I'm familiar with) work.

Just use a single session ID and store multiple session values in it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214 because i want each chat session to be unique and disalbe the sessionkey once a single chat session is closed!!It is kind of difficult case to explain it but my main goad is to have unique session since each session has some data to be stored and identified late for example email chat session to user!
 
No, don't do that at the session ID level.

Start the session.
When the chat starts, set a value in the session.
When the chat ends, clar that value from the session.


You're trying to use the labels on boxes to do the work of things that can be stored in the boxes.



Want the best answers? Ask the best questions! TANSTAAFL!
 
so you mean i generate random value and call that as sessionkey?
 
No.

You instantiate a session using session_start(), then you use that one single session for each user, regardless of what he's doing.

If you need another longish ID for another feature of your website, you can use something like uniqid() to generate that ID, then store that ID in a single session variable in the single session store. The PHP online manual page to which I have linked even has example code that is specific to your application.

Don't use the session-handling mechanism as an ID generator.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for your explainatin. My main goal is id generator .could tell me how these key was generated? Only last 4 digitis are diffrent .It was generated by a pogram a few seconds or min apart but don't know how to it was generated.

H3797254209805864467K57542847
H3797254209805864467K57543761
H3797254209805864467K57544972
 
I can't answer that. How did you generate those IDs?

If you used uniqid(), did you read the online manual page and examine the example code there?



Want the best answers? Ask the best questions! TANSTAAFL!
 
i did not create that. I saw a webpage that generates such number ids for a chatroom and wondering what it represents and how to create one like it.
 
as sleipnir214 points out, php has a function to generate unique ID's.

try something like this
Code:
$uid = getUniqueID("mysite");

function getUniqueID($prefix, $occlude=false){
  if ($occlude){
     return $prefix.sha1(uniqid(rand(), true));
  ]else{
     return uniqid($prefix, true);
  }
}

note that these types of functions cannot guarantee a globally unique ID but they can make collisions very very unlikely. the $occlude flag uses a random number string as a prefix to the unique ID and then encodes the result with sha1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top