Results 1 to 15 of 15

Thread: [RESOLVED] HELP needed for php storing date-time cookie and display

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Resolved [RESOLVED] HELP needed for php storing date-time cookie and display

    hi

    i have written this code for creating a php to store current date and time in a cookie and diaplay last visited on

    while i compile i get an error as "parse error:syntax error,unexpected ':' in D:\documents\d.php on line 3

    <?php
    $inTwoMonths = 60 * 60 * 24 * 60 + time();
    setcookie(‘lastVisit’, date(“G:i – m/d/y”), $inTwoMonths);
    if(isset($_COOKIE['lastVisit'])) {
    $visit = $_COOKIE['lastVisit'];
    echo “Your last visit was – “. $visit;
    }
    else
    echo “You’ve got some stale cookies!”;
    ?>


    please can anyone help me in correcting this code pllzzz

    any help would be appreciated

    thanks

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: HELP needed for php storing date-time cookie and display

    It looks like you copied the code from a Wordpress site, which might have converted the quotes(single and double) with stylish substitutes(curved ones). I just replaced the quotes in your code with normal single and double quotes (the ones that's available in your keyboard). See if it works:
    PHP Code:
    <?php
    $inTwoMonths 
    60 60 24 60 time();
    setcookie('lastVisit'date("G:i – m/d/y"), $inTwoMonths);
    if(isset(
    $_COOKIE['lastVisit'])) {
      
    $visit $_COOKIE['lastVisit'];
      echo 
    "Your last visit was – "$visit;
    }
    else
      echo 
    "You've got some stale cookies!";
    ?>
    BTW, welcome to the forums..

    One more thing, next time when you post code try to enclose it in CODE tags or PHP tags(if the code you are posting in PHP).
    Examples:
    [CODE] your code goes here [/CODE]
    or,
    [PHP] your code goes here [/PHP]

    This improves the readability.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    hi akhilesh

    thanks for the warm welcome....really appreciate it....

    no i did not copy it from word press as u said...but it was given in our college website which i copied and pasted to work out but the code didnt run and i have no idea regarding from where they have taken it.......

    i ran ur corrected code akhilesh but i keep getting only the second sentence which is printed in echo ie

    "you have got some stale cookies"

    even if i reload the page several times i just keep getting the above msg

    i dont get the date and time of the previously visited page......

    plz can u help me with this it tried everything but i cant get this code to work

    plzzz any help to get this code to work will be really thotfull and helpfull for me plzzzz

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    i have one more program for the same

    everything is fine with this program but when i run i get this error as undefined index lvs on line 4

    can u plz help me in correcting the code plz

    PHP Code:

    <?
    $dt=date("l,F,j,Y");
    $ti=date('g:i A');
    if(strcmp($_COOKIE['lvs'],"") == 0)
    {
    $lst="";
    }
    else
    {
    $lst=$_COOKIE['lvs'];
    }
    $lv = $dt . " at " . $ti;
    setcookie("lvs",$lv,time()+3600);
    ?>
    <html>
    <head><title>visitors</title></head>
    <body>
    <?
    print "@@@@@@@@@@@@@@@@@@@@\n";
    if ($lst == "")
    print "<h3> u hav visited first time</h3>";
    else
    print "<h3>$lst</h3>";
    ?>
    </body></html>
    plz any help will be really very gratefull

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: HELP needed for php storing date-time cookie and display

    You can't set and test the cookie in the same code...

    http://php.net/manual/en/function.setcookie.php

    Quote Originally Posted by PHP_Documentation
    Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
    you should have your isset check FIRST.... set a flag... then set the cookie (if I remember right, it has to be set BEFORE any output is sent) ... and then after setting it, check your flag and send out the appropriate output.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    Quote Originally Posted by techgnome View Post
    You can't set and test the cookie in the same code...

    http://php.net/manual/en/function.setcookie.php



    you should have your isset check FIRST.... set a flag... then set the cookie (if I remember right, it has to be set BEFORE any output is sent) ... and then after setting it, check your flag and send out the appropriate output.

    -tg
    thanks for the reply

    can u just help me coding the program....i m completely a noob in php.....

    can u just correct the code what i have posted to make it work or help me build a new one which displays the date and time of the last visit

    plz i m not into php and this code is driving me nuts....

    plz any help will be appreciated in getting this code to run correctly

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: HELP needed for php storing date-time cookie and display

    If you aren't interested in learning, I suggest you hire someone to write the code for you. On this site, you could try the Project Requests section (for unpaid work), or Open Positions (for paid work), but I think you would get more responses elsewhere.
    Last edited by penagate; Nov 1st, 2012 at 09:22 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    i know u r the moderator of this section....

    u dont have to be so rude by giving such a reply for a request...

    i have the code and everything is working fine there is just one error which i have pointed out and so i needed someone who is into php to solve the error......

    thats wat i have even mentioned in my post...

    it dosent mean that i m not interested in learning or something......if someone corrected me with the code i would have learnt where is the mistake i made.....

    and this is not such a job that i should hire someone to write the code for me....

    thanks anyways for the suggestion....

    one more thing consider this as a request plz dont be so rude or give such a lame answer to anyone who is not so good in programming like u guys.....we all r just learning and we asked u guys for a simple help since we are not good in these languages....!!

    hope u understand

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: HELP needed for php storing date-time cookie and display

    OK, noted. As the moderator, may I offer you some advice in turn? If so, I suggest you drop the SMS-style abbreviations and invest some time in capitalising and punctuating your writing: it's a sign of respect to the people who will read your posts and will go a long way towards helping you get the responses you want.

    techgnome offered an explanation for your problem above. He also said, "if I remember right, it has to be set BEFORE any output is sent", which is correct. Here's a quote from the manual page he linked to:
    Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
    Note the mention of whitespace. I see you have a blank line at the beginning of your last code snippet. Make sure that is not present in the actual script file because it will cause the setting of the cookie to fail.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    The blank line was a mistake while copying and pasting it from notepad to the reply....i don't have that in my program....only one single error is present as "undefined index lvs on line 4" ...apart from that the code works perfectly fine....so i needed someone to help me figure out where is the mistake that's all.... so i m in need of some help....

  11. #11
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: HELP needed for php storing date-time cookie and display

    Try this code:
    PHP Code:
    <?php
    $dt
    =date("l,F,j,Y"); //date (eg: Friday,November,2,2012). For more info: http://php.net/manual/en/function.date.php
    $ti=date('g:i A');   //time (eg: 2:01 PM)
    $lv $dt " at " $ti//appending the date and time (eg: Friday,November,2,2012 at 2:01 PM)
    if(!isset($_COOKIE['lvs']))    //check whether the cookie value is not set

        
    $lst="";    
        
    setcookie("lvs",$lv,time()+3600); //since it is not set, we are setting the cookie

    else    
    //if the cookie was set
    {
        
    $lst=$_COOKIE['lvs'];    //retrieve the value from the cookie and save it it in the varibale "$lst"
    }
    ?> 
    <html> 
    <head><title>visitors</title></head> 
    <body> 
        <?php
        
    print "@@@@@@@@@@@@@@@@@@@@\n"
        if (
    $lst == "")
            print 
    "<h3> u hav visited first time</h3>"
        else 
            print 
    "<h3>$lst</h3>"
        
    ?> 
    </body>
    </html>
    Have a look at this to get the idea of cookies: http://www.w3schools.com/php/php_cookies.asp


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: HELP needed for php storing date-time cookie and display

    OK, I swear that code wasn't there when I posted.... anyways... this line is the problem:
    strcmp($_COOKIE['lvs'],"")

    if the lvs item in the cookie doesn't exist, it returns a null value... which you then try to pass to the strcmp ... you need to check to see if it EXISTS first before you can pass it off to something else.
    akhileshbc has kindly provided the code which will do that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: HELP needed for php storing date-time cookie and display

    Thanks a lot akhilesh and tg...

    you guys are really helpful......you guys made this code work at last thanks again guys....you rock....!!!

    thanks a million....

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    16

    Re: [RESOLVED] HELP needed for php storing date-time cookie and display

    and by the way just wanted to say sorry to penagate....i didn't wanted to say what i have said in my post....its just that i got a little out of mind when i read your reply.....so its my sincere apology to you and no offence was meant....

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] HELP needed for php storing date-time cookie and display

    No worries. Glad you got it sorted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width