Nov 22, 2009

How To Use Cookie In Your Web Site ? - this semple code to use and get cookie

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

How To Use Cookie In Your Web Site ? - this semple code to use and get cookie

ejasoft
what is the cookie ?
the cookis it is some info sent and save in user computer

whare i can use the cookies?
becouse the cookies it like the header you can not send it after any output
wes sent so you must send the cookies before any output like as
<html > ,echo and any other code

i well make an E.X. to use the cookies
you must have 2 file
index.php
update.php
----------
in the index.php add this code
CODE
<?
   // This section must go at the top of the page that will display
   // the users favorites.  These are the 'default' URLs that the user
   // will see if they have not edited any.

   if (strlen ($favorite[1]) < 3) setcookie("favorite[1]",  "www.robscripts.com", time()+30000);
   if (strlen ($favorite[2]) < 3) setcookie("favorite[2]",  "www.digitaledge.com", time()+30000);
   if (strlen ($favorite[3]) < 3) setcookie("favorite[3]",  "www.penpalseek.com", time()+30000);
   if (strlen ($favorite[4]) < 3) setcookie("favorite[4]",  "www.budgetideas.com", time()+30000);
   if (strlen ($favorite[5]) < 3) setcookie("favorite[5]",  "www.cardfan.com", time()+30000);

?>

<html>

<head>
<title>Favorites</title>
</head>

<body>
Current Favorites:

<p>

<!-- Start favorites code: This can be put on any php page, anywhere. -->

<?
 for ($x = 1; $x <= 5; print "$x: <a href='http://$favorite[$x]' target='_BLANK'>$favorite[$x]</a><br>", $x++);
?>

<BR><BR>
<a href='update.php'>Update Favorites</a>.

<!-- End favorites code -->

</body>

</html>

in the update.php add this code
CODE
<?

// Location of the page where the users links are displayed.
// Example: [url]http://www.yoursite.com/index.php/[/url]

$loc = "http://www.darpac.com/~robert/favorites/";

// How long do you want the users cookies to save their
// favorite links? In seconds. Day=86400 Week=604800
// month=2419200 year=29030500

$exp = 29030500;

 if ($state == "update") {

    setcookie("favorite[1]",  $fav1, time()+$exp);
    setcookie("favorite[2]",  $fav2, time()+$exp);
    setcookie("favorite[3]",  $fav3, time()+$exp);
    setcookie("favorite[4]",  $fav4, time()+$exp);
    setcookie("favorite[5]",  $fav5, time()+$exp);

    $state = "done";
    header("Location: $loc");
 } else {
   if (strlen ($favorite[1]) < 3) setcookie("favorite[1]",  "www.robscripts.com", time()+300000);
   if (strlen ($favorite[2]) < 3) setcookie("favorite[2]",  "www.digitaledge.com", time()+30000);
   if (strlen ($favorite[3]) < 3) setcookie("favorite[3]",  "www.penpalseek.com", time()+300000);
   if (strlen ($favorite[4]) < 3) setcookie("favorite[4]",  "www.budgetideas.com", time()+300000);
   if (strlen ($favorite[5]) < 3) setcookie("favorite[5]",  "www.cardfan.com", time()+300000);
 }
?>

<html>

<head>
<title>Favorites</title>
</head>

<body>
Update Favorites:

<form method="POST" action="update.php">

<?
 for ($x = 1; $x <= 5; print "$x: <input type='text' name='fav$x' size='29' value='$favorite[$x]'><br>", $x++);
?>

<input type="hidden" value="update" name="state">
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

now run the index.php in your loclhost
and test the cookies have fun

 

 

 


Comment/Reply (w/o sign-up)

JuseH
All the arguments except the name argument are optional. You may also replace an argument with an empty string ("") in order to skip that argument

Note: As of PHP 4, you can use output buffering to send output prior to the call of this function, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : cookie, web, site, semple, code, cookie

  1. Activation Code
    (7)
  2. Php Random Selector
    whats the code (2)
    Is there a PHP script that randomly selects a string from a list? example, the list is: 1-Pie
    2-Balls 3-eggs The script would view a random
    word from those 3 every time i run it. Also is there a function that gives a random number between
    0 and a number i select?....
  3. Dynamic Php Image And Better Php Code Question
    (10)
    Im working on a dynamic image, can i put 2 images in same dynamic image, and can i make this code
    shorter? if ( $goal == 31 ) { $xp2 = ('14833'); } elseif ( $goal == 32 ) { $xp2 =
    ('16456'); } elseif ( $goal == 33 ) { $xp2 = ('18247'); } elseif ( $goal == 34 ) {
    $xp2 = ('20224'); } elseif ( $goal == 35 ) { $xp2 = ('22406'); } elseif ( $goal ==
    36 ) { $xp2 = ('24815'); } elseif ( $goal == 37 ) { $xp2 = ('27473'); } elseif (
    $goal == 38 ) { $xp2 = ('30408'); } elseif ( $goal == 39 ) { $xp2 = ('33648')....
  4. What's Wrong With My Php Webpage?
    there may be something wrong in my php code. (2)
    This is the first time I use the functions fopen() and preg_replace() . It seems that there's
    somthing wrong. Where did I write by mistake? CODE include('data/workinfo/0.php');
    $viewwork = substr($work ,0,249); /* HERE CAN'T WORK WELL! */ $newthread =
    fopen("http://c8s2007.freetzi.com/bbs/new.php?action=article&digest=0&postdate=0&author=1&fname=0&hi
    ts=0&replies=1&pre=6&num=10&length=35&order=1", "rb"); if(!$newthread){         $newthread =
    "无法调用信息!";     }else{         $ad = "
    ";      ....
  5. Php Forum Cookies
    I need help on a php forum cookie. All suggestions welcome. (3)
    OK, I am making a forum in php (with a mysql database). It is much harder then I thought it would be
    but I have been getting along very well. I was just putting some finnishing things on it when I
    found a problem I couldn't figure out. All suggestions are welcome. Problem: I want my forum
    (like all forums) to remember what topics you have already read. I want to do this via cookies and
    at first I was think of having to topic ID to be saved in a string. Example (Cookie:
    %x%) x=the forum topic ID. Then to retrieve that data I would simply look for %x% inside the coo....
  6. Should This Great Site Offer Imagemagick ?
    May be the admin of this site think over it. (3)
    Hi As we all know that this website is very good and offering good services. I just wanna know if
    other people also want ImageMagick to be installed on this server with free accounts... so please
    let me know and lets check if the Admin of this site can isntall it..... Fun....
  7. Code Snippets Repository
    (0)
    Well as everybody know in these forums people posts a lot of code snippets that could be organized
    in a simple page or whatever, my question is if someone is interested to do it or if exists
    something like that here at astahost. I think it would be very helpful for everybody as well to
    keep things more organized. Best regards,....
  8. How Can I Write PHP Code By This Formmail Html
    (5)
    purpose is i want the information in the web page sent into my email. i just know that i need to use
    php script to operate this action ,but i have no idea about this php code. so anyone can help me
    please... thank you very much. ชื่อ
    ....................... ....... นามสกุล
    ....... ชื่อเล่น
    วันเกิด 1 2 3 4 5 6 7 8 9 10 11
    12 13 14 15 16 ....
  9. Dynamic Site Design - Where Do I Start ?
    (7)
    I am new to php. I have some programing background in html, javascript, and c++ but have never done
    anything in php Can someone reccomend some good sites or books etc that can help someone who is
    completely new? My ultimate goal is to make a game like/similar to ponyisland.net....
  10. Need Help Is Adding A PHP Based News Module To My Site
    (2)
    Hey guys i need a simple help i'm builing a homesite and i have a little spot for news. Well i
    just place there the Topics and add a link to another page "news.php". Well its obvious that i dont
    want to build a file for each news that i have so i know that exists a way to work with SQL & PHP. I
    will show want i'm doing CODE        require ('mysql.php');       
    $query="SELECT * FROM News ORDER BY `data` ASC LIMIT 0,5 ";        $result=mysql_query($query);
               $num=mysql_num_rows($result); mysql_close(); echo " Outras Other News "; $i=....
  11. [PHP + MySQL] Separating The Results By Pages
    Simple code (0)
    Hi! I will post here a code for separating the results of MySQL in pages. You ask: Why separete? I
    answer: Imagin that you have 1523 results to display. I dont have to say anything. =P Here is it.
    ------------------------------------------------------------------- CODE $conect =
    mysql_connect("host","user","password"); $select_db = mysql_select_db("database"); $query = "SELECT
    * FROM mytable"; $results = "15"; //Number of results displayed per page. if (!$page) {
        $counter = "1"; } else {     $pcounter = $page; } $start = $counter - 1; $start = $counter *
    $resu....
  12. Multilingual Site: Send The User To Page Of Choice
    (6)
    If you have one site in diferent laanguages, this simple script can redirect the user to the correct
    page acording to his/her language: CODE // Enslish EUA elseif ($HTTP_ACCEPT_LANGUAGE ==
    "en-us"){ header("Location: index_eng.html"); } // Inglês UK elseif ($HTTP_ACCEPT_LANGUAGE ==
    "en-gb"){ header("Location: ingles_enuk.html"); } // Portuguese if ($HTTP_ACCEPT_LANGUAGE ==
    "pt-br"){ header("Location: index_ptbr.html"); } //German elseif ($HTTP_ACCEPT_LANGUAGE ==
    "de-de"){ header("Location: index_ger.html"); } // Swedish elseif ($HTTP_ACCEPT....
  13. How To Remove A Cookie Using PHP
    (1)
    Hi :-) How can i remove a cookie from $_COOKIES array ? P.S.: sorry it there is any mistakes in
    my english :-) Topic title edited to suit the nature of content better. In future try to make
    your topic title more descriptive. ....
  14. Using Bitflags To Restrict Site/page Permissions
    (1)
    My professor is designing a website that uses bit-flag checking to allow access to certain pages.
    You login, validate login, and store their allowed bit flag into a session variable. Then you
    compare to see if they have access or not. It's fairly new to me, but it's apparently very
    common with linux users. Sounds interesting to me, just wondering if any one has used this, or is
    it a little too much for simple pages. His site however is going to be more of "software" for
    several users. Is it very secure and does it work well?....
  15. What's Wrong With This Preg_replace Code?
    (2)
    I have written a JavaScript that prompts auser for 5 table attributes for entry into the posting
    area and it produces this: It is then passed through this preg_replace for display in the
    preview or as a submission in a post: CODE $txt = preg_replace( "#\ (.+?)\ #is", " \\6 ", $txt
    ); What it produces however is this: CODE When what I want is this: CODE
    What is it that is wrong with this code and giving this odd unwanted result? Help would be most
    certainly appreciated because I am tired of looking at it, my eyes are getting crossed ....
  16. Any Tool/software For Php Code Generation ?
    Tool for code generator (3)
    Where find any tool to generate PHP class?....
  17. Do You Want A Mail Form In Your Site
    (2)
    Notice from m^e: Repeat post. Credits reduced by 5 days. Learn to USE
    THE SEARCH BUTTON before you make such posts. did you want to have in your web site mail form
    that allow the user to send mails to anther mail from his mail e.g. the compose in yahoo CODE
    from to cc bcc subject
    function param($Name)         {         global
    $HTTP_POST_VARS;         if(isset($HTTP_POST_VARS ))            return($H....
  18. Php Reading And Writing To File
    the code is very esay (4)
    this code to Read from file you can use this code in to make a small data base and it is very to
    use CODE $fp = fopen ("file.txt", "r"); $bytes = 4; $buffer = fread($fp, $bytes); fclose
    ($fp); print $buffer; ------------------- to write to same file CODE $fp = fopen
    ("file.txt", "w+"); fwrite ($fp, "Test"); fclose ($fp); ---------------- thanks and iwait the
    commant....
  19. Own Links On Site
    (6)
    I'm thinking of having some kind of system so the users can register and then put up their
    links. Everything should be saved in some kind of database. Anyone now any tutorial or guide or
    something like that of how to do that?....
  20. PHP Based Site Access Authentication - Help
    How to block parts of your web-site ?? (4)
    How can i program my web page using php that when the value of the login box is equal to some string
    then go to my success.html otherwise on my fail.html????help me guys!
    ------------------------------------ It would help the readers far better to understand what your
    problem is - if you state the nature of it in short in your topic title, instead of just "Php help".
    It'll also get you a lot more responses. Am changing your topic title to give you an example.
    All the best /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> m^e....
  21. Site Counters - Help Needed
    (13)
    I want a good Site counter(to keep track of visitors) for my site..... can nebody temme.....
    where will i get free stuff on this... (dont ask me to google..and try out..i am doing that..just in
    case u know someplace) OR Can how do i design my own (which seems quite....difficult for me) Plz
    help!! Satya....
  22. Php Script To Download File From Another Site
    (11)
    hi i need a php or java script code for downloading files from other sites to my site for example:
    http://download.com/file.zip to http://mysite.com/file.zip thanks....
  23. Page Doesn't Load When This Php Code Is Present ?
    (6)
    CODE switch($_GET ) {   case "home";      include("index.php");   break;   case
    "gallery";      include("gallery.php");   break;     case "links";      include("links.php");
      break;   case "guestbook";      include("../guestbook/");   break;   case "portfolio";  
       include("portfoilio.php");   break;   case "other";      include("other.php");   break;
      case "about";      include("about.php");   break;   case "contact";    
     include("contact.php");   break;   case "login";      include("/private/");   break;  
    default:....
  24. How Do U Make Members Only Web-site
    (7)
    how do u make it that only a member of that site can view that page?....

    1. Looking for cookie, web, site, semple, code, cookie

See Also,

*SIMILAR VIDEOS*
Searching Video's for cookie, web, site, semple, code, cookie
advertisement



How To Use Cookie In Your Web Site ? - this semple code to use and get cookie

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com