Question:
What is the php code for entering a "session_start" timestamp into a database table?
Colin C
2009-02-08 19:23:16 UTC
I want to insert the data (timestamp) into a "session-start" column in a table called "student" in a database called "frog". I don't know how to write the code. Can anybody tell me? I have been told to write
9 10 session_start()
11 require "sajax.php"
12 ?>
but this just brings up the following scary warning - "session_start() [function.session-start]:Cannot send session cookie - headers already sent by output started at ...........line 10" Grateful of some help? YOU BET!!
Four answers:
Tikken
2009-02-08 19:30:29 UTC
session_start() needs to be called before any output are sent to the browser, both headers and content. So include it at the top of your script to be sure.



Also make sure that you have added the ending ";" semicolon.

-----StartCode-----

$TimeStamp = time();

mysql_query("INSERT INTO student (Sname, Sdata) VALUES (

'TimeStamp',

'$TimeStamp')", $Connection) or die(mysql_error());

-----CodeEnd-----

Remember that the time function will be in the unix format, this means you have to do something like below to output the time in a "readable" date format.

-----StartCode-----

echo date("d/m/Y", $TimeStamp');

-----CodeEnd-----



It is generally best to post the Unix date format in the database, as its easier to work with then "readable" formats, simply use above whenever you are going to output date/time to the browser.



See also:

http://www.php.net/session

http://www.php.net/time
2009-02-08 19:54:04 UTC
You can not send a session_start() if you have already sent ANY headers, which is anything sent by the section, or anything else. Even a space. This must be set before anything else on the page. You will need something like a blank page starting with :


session_start();

require "whatever.php";

$sess_time = time;

$conn = mysql_connect("localhost","datauser","datapassword");

mysql_select_db("frog",$conn);

/* You will need to write a routine to make the date variable a date in the format yyyy-mm-dd (2009-02-09) if you ever want to be able to use date order sorting. You the store this in a field of date type*/

$insert_res = mysql_query("insert into student (sessionstart) values('$modified_date')",$conn);

/* Then build your page */

$display_string ="Some html to build the page including divs etc";

?>

Your page title









2009-02-08 19:36:39 UTC
Something somewhere in your code is sending output (most likely an echo or two) before session_start() is called. session_start() sends header information for inclusion in the outputted page. Since header information must be sent BEFORE all other output, you need to stop the other output temporarily to allow the headers to be outputted first. Do this by adding an ob_start() at the beginning of your code and an ob_end_flush() at the end.
Rasmah.com
2009-02-08 19:32:02 UTC
At first your lucky man because i answer you .



At first don't write


And the first line must be session_start() before any thing you must add this in the top of index file.



Now i will answer you about how to add info to session.



Ex :

$_SESSION['name of your session'] = $varible // Any data you want to add it to you're session.



And now i think you want to know how you can use this session ?



Ex:

echo $_session['name of you're session'];

//That will give you the content of this session .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...