<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: php code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 07:22:48 GMT</pubDate>
    <description>DZone Snippets: php code</description>
    <item>
      <title>str_hex and hex_str</title>
      <link>http://snippets.dzone.com/posts/show/2039</link>
      <description>// Convert hex to string and vice versa.&lt;br /&gt;//&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function str_hex($string){&lt;br /&gt;    $hex='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($string); $i++){&lt;br /&gt;        $hex .= dechex(ord($string[$i]));&lt;br /&gt;    }&lt;br /&gt;    return $hex;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function hex_str($hex){&lt;br /&gt;    $string='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($hex)-1; $i+=2){&lt;br /&gt;        $string .= chr(hexdec($hex[$i].$hex[$i+1]));&lt;br /&gt;    }&lt;br /&gt;    return $string;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// example:&lt;br /&gt;&lt;br /&gt;$hex = str_hex("test sentence...");&lt;br /&gt;// $hex contains 746573742073656e74656e63652e2e2e&lt;br /&gt;&lt;br /&gt;print hex_str($hex);&lt;br /&gt;// outputs: test sentence...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 01:41:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2039</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
    <item>
      <title>Simple snoopy example</title>
      <link>http://snippets.dzone.com/posts/show/2007</link>
      <description>// Shows how an example how you can use the Snoopy class for doing HTTP requests to other websites.&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/ - Public domain)&lt;br /&gt;// &lt;br /&gt;// You need the Snoopy class from http://snoopy.sourceforge.net/ for this snippet&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;include("snoopy.class.php");&lt;br /&gt;&lt;br /&gt;$snoopy = new Snoopy;&lt;br /&gt;&lt;br /&gt;// need an proxy?:&lt;br /&gt;//$snoopy-&gt;proxy_host = "my.proxy.host";&lt;br /&gt;//$snoopy-&gt;proxy_port = "8080";&lt;br /&gt;&lt;br /&gt;// set browser and referer:&lt;br /&gt;$snoopy-&gt;agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";&lt;br /&gt;$snoopy-&gt;referer = "http://www.jonasjohn.de/";&lt;br /&gt;&lt;br /&gt;// set some cookies:&lt;br /&gt;$snoopy-&gt;cookies["SessionID"] = '238472834723489';&lt;br /&gt;$snoopy-&gt;cookies["favoriteColor"] = "blue";&lt;br /&gt;&lt;br /&gt;// set an raw-header:&lt;br /&gt;$snoopy-&gt;rawheaders["Pragma"] = "no-cache";&lt;br /&gt;&lt;br /&gt;// set some internal variables:&lt;br /&gt;$snoopy-&gt;maxredirs = 2;&lt;br /&gt;$snoopy-&gt;offsiteok = false;&lt;br /&gt;$snoopy-&gt;expandlinks = false;&lt;br /&gt;&lt;br /&gt;// set username and password (optional)&lt;br /&gt;//$snoopy-&gt;user = "joe";&lt;br /&gt;//$snoopy-&gt;pass = "bloe";&lt;br /&gt;&lt;br /&gt;// fetch the text of the website www.google.com:&lt;br /&gt;if($snoopy-&gt;fetchtext("http://www.google.com")){ &lt;br /&gt;    // other methods: fetch, fetchform, fetchlinks, submittext and submitlinks&lt;br /&gt;    &lt;br /&gt;    // response code:&lt;br /&gt;    print "response code: ".$snoopy-&gt;response_code."&lt;br/&gt;\n";&lt;br /&gt;    &lt;br /&gt;    // print the headers:&lt;br /&gt;    &lt;br /&gt;    print "&lt;b&gt;Headers:&lt;/b&gt;&lt;br/&gt;";&lt;br /&gt;    while(list($key,$val) = each($snoopy-&gt;headers)){&lt;br /&gt;        print $key.": ".$val."&lt;br/&gt;\n";&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    print "&lt;br/&gt;\n";&lt;br /&gt;    &lt;br /&gt;    // print the texts of the website:&lt;br /&gt;    print "&lt;pre&gt;".htmlspecialchars($snoopy-&gt;results)."&lt;/pre&gt;\n";&lt;br /&gt;    &lt;br /&gt;}&lt;br /&gt;else {&lt;br /&gt;    print "Snoopy: error while fetching document: ".$snoopy-&gt;error."\n";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 May 2006 02:40:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2007</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
    <item>
      <title>WWW-Authenticate example</title>
      <link>http://snippets.dzone.com/posts/show/2006</link>
      <description>// Shows how to use the WWW-Authenticate header to make login pages.You find a good tutorial at php.net&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/ - Public domain)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;$login_successful = false;&lt;br /&gt;&lt;br /&gt;// check user &amp; pwd:&lt;br /&gt;if (isset($_SERVER['PHP_AUTH_USER']) &amp;&amp; isset($_SERVER['PHP_AUTH_PW'])){&lt;br /&gt;&lt;br /&gt;    $usr = $_SERVER['PHP_AUTH_USER'];&lt;br /&gt;    $pwd = $_SERVER['PHP_AUTH_PW'];&lt;br /&gt;&lt;br /&gt;    if ($usr == 'jonas' &amp;&amp; $pwd == 'secret'){&lt;br /&gt;        $login_successful = true;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// login ok?&lt;br /&gt;if (!$login_successful){&lt;br /&gt;&lt;br /&gt;    // send 401 headers:&lt;br /&gt;    // realm="something" will be shown in the login box &lt;br /&gt;    header('WWW-Authenticate: Basic realm="Secret page"');&lt;br /&gt;    header('HTTP/1.0 401 Unauthorized');&lt;br /&gt;    print "Login failed!\n";&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;else {&lt;br /&gt;    // show secret page:&lt;br /&gt;    print 'you reached the secret page!';&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 May 2006 02:33:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2006</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
  </channel>
</rss>
