Results 1 to 10 of 10

Thread: Cookie Help Required

  1. #1

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Cookie Help Required

    Hi,

    I want to store a cookie in user's system which will stay there until they remove it from their browser by "Clearing Cookies". Please tell me how do i do it ?

    Thanks.
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cookie Help Required

    You need to use the setcookie() function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Re: Cookie Help Required

    Hi visualAd,

    Thanks for the tip, i figured it out but now i am facing another problem. I setup 2 cookies...for example:

    If user comes to page1.php i set a cookie: testcook1 with value step1
    then user is taken to next page page2.php which sets another cookie named testcook2 with value step2

    now after all 2 cookies are set i want to display the cookies value:

    So the testcook1 is displaying successfully but the testcook2 is not showing its value ...can you please tell me what i am doing wrong with this one ? All the code is same as testcook1...just names changed etc. and still its not working please tell me why..

    Thanks
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  4. #4
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Cookie Help Required

    Without seeing code, not sure, but this is a very basic example:
    page1.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    $value 
    'Test 1';
    setcookie("TestCookie"$value);
    ?>
    <a href="page2.php">Next page</a>
    </body>
    </html>
    page2.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    $value 
    'Test 2';
    setcookie("TestCookie2"$value);
    ?>
    <a href="page3.php">Next page</a>
    </body>
    </html>
    page3.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    echo $_COOKIE['TestCookie'] . "<br>" $_COOKIE['TestCookie2'];
    ?>
    </body>
    </html>
    It'd easier to help if you post up your code...

  5. #5

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Re: Cookie Help Required

    Hi,

    Cani see them on page3 only or can they be seen on page2 only ? I am setting at page1 then at page 2 and after setting on page2 i am displaying them both but only 1 is displaying and 2 is not.

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cookie Help Required

    A cookie is probably best described as a small information token. After the cookie has been set by the server, the client sends it back thereafter with each request. The parameters for the setcookie() function allow you to specify the cookies constraints, i.e: path, domain, secure. These tell the client which pages it should pass the cookie back to.

    In answer to your question a cookie is valid for all pages within the specified path and/or the domain. If none are given its available globally throughout your site. It is worth however noting that the cookie will not be visible until the page request after you have set it.

    If you are thinking of storing a lot of information inside the cookie, or find yourself needing more than one, or will be using it to identify users then I would recommend you use PHP's built in session module instead.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Re: Cookie Help Required

    Hi,

    I shifted to cookies becuase i already tried session. Anyways do i have to put session_start() on top of the every page ?

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cookie Help Required

    Yes, when you start a session for the first time it attempts to set a cookie, so you should use the session_start() function like you would use the setcookie() function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Cookie Help Required

    Quote Originally Posted by Brandoe85
    Without seeing code, not sure, but this is a very basic example:
    page1.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    $value 
    'Test 1';
    setcookie("TestCookie"$value);
    ?>
    <a href="page2.php">Next page</a>
    </body>
    </html>
    page2.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    $value 
    'Test 2';
    setcookie("TestCookie2"$value);
    ?>
    <a href="page3.php">Next page</a>
    </body>
    </html>
    page3.php
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    echo $_COOKIE['TestCookie'] . "<br>" $_COOKIE['TestCookie2'];
    ?>
    </body>
    </html>
    It'd easier to help if you post up your code...
    That code will not work, because you have called the setcookie() function after output as started. It should be at the very top of the page before and other echo or print statements and before and HTML.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459

    Talking Re: Cookie Help Required

    Hi,

    Okay thanks i will try it out and let you know if i get any problems.

    Thanks.
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

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