Page 1 of 1
PHP Timer
Posted: Tue Jan 10, 2006 6:09 pm
by pauspling
Hi guys!
I have created a php/mysql survey. Now, I would like to add a timer function that disables the survey 30 minutes after being accessed for the first time. Is that possible?
/Paulina
Posted: Tue Jan 10, 2006 6:39 pm
by feyd
disabled permanently (for all users) or disabled for that particular user? Is it anonymous or does it require registration?
php/mysql
Posted: Tue Jan 10, 2006 8:06 pm
by pauspling
Oh my. I haven't thought of that. I am all new to databases.
I asked a friend of mine to log on to my survey: 18.85.23.136/survey_html.php. But hes said, he got an error message saying: "Could not add data to the database". Is that because he tries to access it from outside? Do I need to provide registration. My intention is to make it open to anyone. I will only send the link to two people, and I want to disable the survey for them.
Posted: Tue Jan 10, 2006 8:33 pm
by raghavan20
i tried the survery, i am not able to add to the database if the fields are not filled properly but adds a record if all the fields are filled in properly.
I will only send the link to two people, and I want to disable the survey for them.
can you be more clear on what you saying because I could not make out from what you have said. Try to explain it with an example.
Posted: Tue Jan 10, 2006 8:34 pm
by feyd
the error would likely be code specific, so we'd need to see your code.
As for disabling, you could make a semi-registration. Here's what I'm thinking:
Have a table for open surveys. In this table you have a field that I'll call a survey code. This code is a completely randomly generated string of basic nonsense like that kicked out of md5() or base64_encode(). Another field is the survey it applies to, a reference to a surveys table for example.
To take a survey, someone needs to enter the survey code. Submitting the correct one will open the survey it applies to for them to fill out. When someone starts a survey, their entry in the open survey table is either removed, or marked as being "in-use."
A piece of Javascript on the survey page makes it time out (expire) after the time alotted (however, I'd suggest against this as it places some pressure on the person taking it to complete it quickly.)
Php/mysql timer
Posted: Wed Jan 11, 2006 9:47 am
by pauspling
Hi, thanks for your help. I am using the surveys in a user study, where I want the users to fill in the survey about their current activities and state of mind within 30 min from accessing the survey (to make sure that the answers are accurate and "up-to-date"). In other words, I would liek to disable the "send" function of the survey after 30 min. Instead of "Data has eben added to the dabase", the user gets "Your waiioted too long". Can I use the time() function somehow?
/Paulina
Posted: Wed Jan 11, 2006 11:04 am
by feyd
easy enough. Store the timestamp (time()) of when a survey code is opened. When a submission happens, check the current time against their survey code's start time. If exceeded, tell them they were too slow.
Php/mysql timer
Posted: Wed Jan 11, 2006 1:20 pm
by pauspling
OK. Actually I tried that. However, I am using to separate php files; one that displays the survey, and one that adds the data to the database when the survey is submitted. I managed to store the end_time in file nr 2 in the database, but when I tried to store the start_time in file nr 1, it stored NULL. So, what I am doing right now, is saving both in the second file (se below), which obviously is wrong.
Code: Select all
<?php
list($usec, $sec) = explode(' ', microtime());
$script_start = (float) $sec + (float) $usec;
list($usec, $sec) = explode(' ', microtime());
$script_end = (float) $sec + (float) $usec;
// Connect to DB here
$MySQL_Server = "localhost";
$MySQL_User = "username";
$MySQL_Password = "password";
$MySQL_Database = "survey_questions";
$MySQL_Table = "survey_1_result";
mysql_connect($MySQL_Server, $MySQL_User, $MySQL_Password);
mysql_select_db($MySQL_Database);
$q1 = $_POST["q1"];
$q2 = $_POST["q2"];
$q3 = $_POST["q3"];
$q4 = $_POST["q4"];
$q5 = $_POST["q5"];
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip = getenv('HTTP_X_FORWARD_FOR');
$host = gethostbyaddr($ip);
} else {
$ip = getenv('REMOTE_ADDR');
$host = gethostbyaddr($ip);
}
// set the default timezone to use. Available since PHP 5.1
//date_default_timezone_set('UTC');
$date = date( 'H:i:s l Y-m-j', time() );
//$date = date('l dS of F Y h:i:s A');
//$data = NULL;
//print $data;
$query = "
INSERT INTO $MySQL_Table (user, ip, date, start_time, end_time, q01, q02, q03, q04, q05, email, conf_num, dept)
VALUES ('$host','$ip','$date','$script_start','$script_end', '$q1', '$q2', '$q3', '$q4', '$q5',
'$email', '$conf_num', '$dept')
";
$result = mysql_query($query);
//added
if ($result) {
?>
<p>Thank you. Your survey was added to the database ...<br />
<?php
} else {
?>
<p>Your survey could not be added to the database.
<?php
}
?>
Posted: Wed Jan 11, 2006 2:32 pm
by pilau
I'd really suggest to hide your DB connection details.
Php/mysql timer
Posted: Wed Jan 11, 2006 3:04 pm
by pauspling
Oh thanks.
