Assigning a session to user

hi all,
i have written a code in php that will check whether the session exists
or not and it will generate the session id.
below is the code…


<?php 
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");

if(!isset($_SESSION['username']))
{
 print "Your SessionID: ".time();
}
else
{
 print "Session does not exist";
}
?>

now i want that session id to be assigned to each user.
tell me how to do it…

$time = time();
print "Your SessionID: ".$time;
$_SESSION[‘username’] = $time;

whether time() is equal to PHPSESSID?

What do you think?

time will never be equal to the session id. Your code said you were making the “session ID” = time.


 print "Your SessionID: ".time(); 

Right there.

If you’re trying to find out what the session id is, use [FPHP]session_id[/FPHP] (isnt it amazing how so many of the commands are actually named exactly what you’re looking for, and a simple manual search would find them?)

i think both are same…
is that so


echo "Time: ".time()." SessionID: ".session_id();

You tell me.

time generates the seconds elapsed since 1971(not known exactly)
whats the no PHPSESSID generates

It’s the Session ID. It’s a (mostly) unique hash for each session.

when the session is closed for a particular code then what the new session
will generates next time the same program is opened again

A completely different hash.

on what basis it will calculate the number

Far as i know it’s random.

when we are working with session whether we need to consider the database
and is it possible to apply the sessions to database

Why?
Database not necessary. Ignore database.
What is it with people lately and wanting to stuff everything inside a database?

k i have got session id with the above code.
now i need to re direct to another page called “products.php” which contains 3 products
how to add that extra thing…

add a line of code with a header()

either [FPHP]include[/FPHP] it (recommended) or [FPHP]header[/FPHP] the user to the location.

i have done.
but it is showing the error Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\sess2.php:8) in C:\xampp\htdocs\sess2.php on line 10

below is the location added code


<?php 
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
if(isset($_SESSION['username']))
{
 $time = time();
 print "Your SessionID: ".$time;
 $_SESSION['username'] = $time;
 header("Location:products.php");
 
}
else
{
 print "Session does not exist";
}
?>

You cant send a header() after printing something to the screen. (It wont be visible after you change the location, anyway, so you might as well not print it)