PHP cURL Cookies Example

Posted by Mike Lopez under PHP cURL
No Comments
May 2008
20
01:01pm

Want to learn how to use PHP cURL with Cookies? Well, it’s pretty simple so let’s go see the code but first let’s make a scenario…

Say we have a page called ‘cookiepage.php’ in a particular website that checks for the cookie set by the homepage and will only return the proper output if the cookie exists. How do you do it? Here’s how…

<?php
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

/* here you can do whatever you want with $output */
?>

Just in case you weren’t able to catch what we did in the above example, here’s a short explanation.

  • STEP 1 creates a temporary cookie file using the tempnam() function.
  • STEP 2 loads the homepage and saves the cookie in our temporary cookie file. The key here is this line:

    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);

  • STEP 3 loads cookiepage.php with the saved cookie with this line:

    curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);

Sleek and simple ei? Hope this PHP cURL Cookie example helped you out!


Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
May 2008
20
12:10pm

Do you need to post data to a website using PHP? It’s actually pretty easy to do. Here’s the code.

$urltopost = "http://somewebsite.com/script.php";
$datatopost = array (
"firstname" => "Mike",
"lastname" => "Lopez",
"email" => "my@email.com",
);

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
May 2008
20
12:04pm

Do you want to learn how to use cURL in PHP? Well, if so then this tutorial is for you. But before anything else, what is cURL?

cURL is a command line tool for transferring files with URL syntax. The strong point of cURL is the number of data transfer protocols it supports. It is distributed under the MIT License which makes cURL free software. It supports FTP, FTPS, HTTP, HTTPS, TFTP, SCP, SFTP, Telnet, DICT, FILE and LDAP. - based on Wikipedia’s definition

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb

PNG images are more often better than GIF when it comes to rendering images with transparency mainly because GIF can only support 256 colors while PNG can support 32-bit colors. Here’s a graphical comparison between the two.

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb

I’m a Potentate of the Rose

Posted by Mike Lopez under Blog
No Comments
Apr 2008
18
09:41pm

Be a Potentate of the Rose by playing Petals of the RoseOh yes! I finally cracked the code after spending almost an hour trying to figure the logic of the puzzle and I officially became a Potentate of the Rose. The game is called Petals Around the Rose and it’s quite a nasty game. My friend Kiel introduced me to it and I was somehow perplexed at how he came up with the right answers until I finally “got it!”

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Mar 2008
26
07:34pm

I realized that a lot of people who want to learn PHP are having hard time getting started with it - not because PHP is difficult to use or understand but simply because they don’t know where to start. Because of this, I decided to write this short how-to on getting started with PHP. It covers all the necessary stuff to setup Apache, PHP, and MySQL as well as to install one of my favorite PHP editors.

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Mar 2008
22
09:55am

As promised, here’s a sample implementation of the Google Translate API.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Google Translate</title>
<script language="javascript" src="http://www.google.com/jsapi"></script>
<script language="javascript">
 google.load ("language", "1");
 function gtrans (f) {
  document.getElementById("translation").innerHTML = "-";
  var lang = f.langpair.value.split("|");
  google.language.translate (f.translate.value, lang[0], lang[1], function (result) {
   if (!result.error) {
    var container = document.getElementById("translation").innerHTML = result.translation;
   }
  });
  return false;
 }
</script>
</head>
<body>
<form onsubmit="return false">
 <p>Enter text to translate and click "Translate"</p>
 <textarea name="translate" cols="50" rows="5"></textarea>
 <br />
 <select name="langpair">
  <option value="ar|en">Arabic to English</option>
  <option value="zh|en">Chinese to English</option>
  <option value="zh-CN|zh-TW">Chinese (Simplified to Traditional)</option>
  <option value="zh-TW|zh-CN">Chinese (Traditional to Simplified)</option>
  <option value="nl|en">Dutch to English</option>
  <option value="en|ar">English to Arabic</option>
  <option value="en|zh-CN">English to Chinese (Simplified)</option>
  <option value="en|zh-TW">English to Chinese (Traditional)</option>
  <option value="en|nl">English to Dutch</option>
  <option value="en|fr">English to French</option>
  <option value="en|de">English to German</option>
  <option value="en|el">English to Greek</option>
  <option value="en|it">English to Italian</option>
  <option value="en|ja">English to Japanese</option>
  <option value="en|ko">English to Korean</option>
  <option value="en|pt">English to Portuguese</option>
  <option value="en|ru">English to Russian</option>
  <option value="en|es">English to Spanish</option>
  <option value="fr|en">French to English</option>
  <option value="fr|de">French to German</option>
  <option value="de|en">German to English</option>
  <option value="de|fr">German to French</option>
  <option value="el|en">Greek to English</option>
  <option value="it|en">Italian to English</option>
  <option value="ja|en">Japanese to English</option>
  <option value="ko|en">Korean to English</option>
  <option value="pt|en">Portuguese to English</option>
  <option value="ru|en">Russian to English</option>
  <option value="es|en">Spanish to English</option>
 </select>
 <input type="button" value="Translate" onclick="gtrans(this.form)" />
 <p id="translation" style="border:1px solid #00f;padding:5px;width:400px">-</p>
</form>
</body>
</html>

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Mar 2008
21
10:41am

If you need to translate your website from one language to another then I have some good news for you. Google Translate has released a JavaScript based API that’s both easy to use and implement. It can handle 13 different languages and as much as 29 language pairs.

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Mar 2008
17
09:13pm

Form input validation is sometimes an annoying part in web development but it’s something that we web developers can’t avoid. In order to make sure that the data being entered by our users are valid, we simply have to validate. Another good use of validation is to provide feedback to the user on whether what they are typing is acceptable or not.

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb

AJAX Introduction

Posted by Mike Lopez under Javascript/AJAX
2 Comments
Mar 2008
17
08:25pm

Ever wondered what AJAX is? AJAX stands for Asynchronous JavaScript and XML. It is not a programming language in itself but rather a combination of different web development techniques. Furthermore, AJAX technology is not new though it has only been popularized recently especially by Google (Google Maps, GMail, Google Calendar, etc.)

Read the rest of this entry »

Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb