GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2008, 17:39
partnole partnole is offline
Junior Member
 
Join Date: Jun 2007
Posts: 30
partnole is on a distinguished road

Need help to display time & date


Please tell me what I am missing from my code. It works but it does not display the time and date.
Thanks


PHP Code:

<?php
 if(isset($_COOKIE['webSurvey']))
    
 {
 print("Your last submision time was ");
 
 die("<br />Please wait for 24 Hours before retry!");
 }
 else
 {
 extract( $_POST );
 setcookie( "webSurvey", $name, time() + 60*1);
 setcookie("time", time(), time() + 60 * 1);
}
?>

  #2  
Old 04-Jul-2008, 12:41
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 785
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Need help to display time & date


Well that looked like an interesting excercise. I got no output at all.
So I took some time and went over things like date() and cookies in the http://www.tizag.com/phpT/ tutorial and found that there were several problems with what was posted.
I put together the following which attempts to demonstrate some concepts and more proper (I hope) methods.

But there is one thing I do not understand and that is the concept of the php 'header area'.
Where is it? What types of statements MUST be placed there?
Maybe someone can enlighten me.
PHP Code:

<?php
  /*** I found I must have the setcookie first or I would get errors like
       'Warning: Cannot modify header information - headers already sent by...'
  
       So where is that 'header end' cut-off line ???
  ***/
  $ptime = time();
  $waitime = 24 * 60 * 60;
  $expiretime = $waitime + time();
  
  if(!isset($_COOKIE['webSurvey'])) /* don't want to reset the waiting period */
  {  
    setcookie("webSurvey", time(), $expiretime);
    echo "setting cookie <br /><br />";  
  }  
  echo $ptime . " - was the output of time(). <br /><br />";
  echo $expiretime . " - is the value held in \$expiretime. <br /><br />";
  
  /*** Then proceed with this kind of stuff
  ***/
  if(isset($_COOKIE['webSurvey']))
  { 
    $lastime = $_COOKIE['webSurvey'];
    echo $lastime . " - was your last submision time. <br />";
    $remainingtime = $lastime + $waitime - $ptime;
    echo " - - - - - - <br />
         Please wait another $remainingtime seconds <br />
         (or " . $remainingtime / 60 / 60 . " hours) <br />";
  }
  else         /* no cookie found so 24 hr 'expiretime' has passed */
    echo "Proceed with your submission... <br /><br />";
 
  echo "<br /> - - - The End - - - <br />";  
?>

P.S. : I tried to use php codebox tags but it seemed display only as one continuous string. (no newlines)
I tried pasting from vi mouse select and gedit both mouse and ctrl-a ctrl-c gui style... what am I doing wrong?
Last edited by admin : 27-Mar-2009 at 03:51. Reason: fixed bbcodes
  #3  
Old 26-Mar-2009, 16:20
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Re: Need help to display time & date


Quote:
Originally Posted by partnole
Please tell me what I am missing from my code. It works but it does not display the time and date.
Thanks

Here are some improvements to the script. Read the code comments for details.
PHP Code:

<?php
/*
 * This does not keep someone from clearing
 * their cookies and resubmitting the form
 */
if (isset($_COOKIE['webSurvey']))
{
    if (isset($_COOKIE['time']))
    {
        // Converts timestamp to formatted date
        printf('Your last submision time was %s.<br />'
              , date('Y-m-d H:i:s', $_COOKIE['time']));
    }
    echo 'Please wait for 24 hours before retry!';
}
else if (isset($_POST['btnSubmit'])) // The form was submitted
{
    // Creates $name and $btnSubmit
    // Vulnerable to injection attacks; Consider using $_POST['name'] instead
    extract($_POST);
    
    // The webSurvey cookie is not set if $name is an empty string
    if (empty($name))
        $name = 'anonymous';
    
    // More efficient than calling time() three times
    $time = time();
    
    // Cookies will expire in 24 hours (60 * 60 * 24 = 86400)
    setcookie('webSurvey', $name, $time + 86400);
    setcookie('time', $time, $time + 86400);
    
    printf('<p>Thank you, %s.</p>', $name);
}
else // No cookie, no form submitted
{
?>
    <form method="post" action="">
        Name: <input type="text" name="name" value="" />
        <input type="submit" name="btnSubmit" value="Submit" />
    </form>
<?php
}
?>

<pre style="font-size: smaller">
For Debugging:
$_POST =&gt; <?php print_r($_POST); ?>
$_COOKIE =&gt; <?php print_r($_COOKIE); ?>
</pre>
  #4  
Old 26-Mar-2009, 16:31
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Re: Need help to display time & date


Quote:
Originally Posted by Howard_L
But there is one thing I do not understand and that is the concept of the php 'header area'.
Where is it? What types of statements MUST be placed there?
Maybe someone can enlighten me.
The header is invisible, but it contains meta data, data about the file. In Firefox, you can see some of the kinds of things that are found in the header by using the Page Info feature. It's usually in the Tools menu. In PHP, you can change things in the header before it is sent by using header(). Like setcookie(), header() must come before anything is sent to the browser -- in other words, before the header is sent.
PHP Code:

<?php
header('Content-type: text/css');
echo 'body { background-color: black; }';
?>

PHP automatically sends a header just before anything is sent to the browser. In the next example, this happens when echo is used. Because setcookie() has to be called before the headers are sent, an error occurs.
PHP Code:

<?php
/*
 * This is wrong!
 */
echo 'Output before setting cookie.';
setcookie('index', 'value', time() + 86400);
?>

PHP Code:

<?php
/*
 * This is correct
 */
setcookie('index', 'value', time() + 86400);
echo 'Output after setting cookie.';
?>

PHP Code:

<?php
/*
 * This is also correct
 * Even though echo comes first in the code,
 * it will not be executed before setcookie()
 */
$a = true;
if ($a)
    echo 'Output without setting cookie.';
else
    setcookie('index', 'value', time() + 86400);
?>

  #5  
Old 26-Mar-2009, 16:37
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Re: Need help to display time & date


Quote:
Originally Posted by Howard_L
P.S. : I tried to use php codebox tags but it seemed display only as one continuous string. (no newlines)
I tried pasting from vi mouse select and gedit both mouse and ctrl-a ctrl-c gui style... what am I doing wrong?
This might have been a problem with the PHP highlighter that was later fixed, or it might be that you used the Quick Reply feature instead of the Advanced version. I noticed that the Quick Reply took the newlines out of my code when I used it to edit a post.
  #6  
Old 27-Mar-2009, 03:55
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 834
admin will become famous soon enough

Re: Need help to display time & date


Yes, this is true. When replies (with example PHP script) are posted via Quick Reply, all newlines/carriage returns seem to disappear. I never figured it out, or I just didn't try hard enough.

Even so, I am preparing to update the site any day now, and all these bugs should be gone, or replaced by completely new ones!
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write the declaration for a Date object Sosy C++ Forum 6 26-Oct-2007 10:21
constructors/classes mapes479 C++ Forum 3 19-Nov-2006 18:34
Limit combo box and date time picker choice according to database created in folder shinyhui C++ Forum 0 22-Feb-2005 21:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 03:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.