Categories

Archives

Syndication


External content in DIV

4
Oct
Posted at 6:45 am in Internet, PHP

Ever thought of having external content in a DIV without using iframes?

Iframes are ugly and will not validate properly in strict XHTML so therefore a solution for this is needed.
The solution we will use is a very simple PHP function that will grab the content from the page and then display it properly.

So how do we replace that iframe with something else? How can we create an iframe without an iframe?

This solution is widely used as proxy-scripts around the web and it is by far the most simple.

If you’re only aiming to replace an iframe you can do it easily with the file_get_content PHP function.

1
2
3
4
5
6
7
<?php
function displaycontent($url)
{
$content = file_get_content($url);
echo $content;
}
?>

And if you are aiming for a simple proxy-script, you can just add a str_replace command and replace all hrefs with some additional characters:

1
2
3
4
5
function displaycontentproxy($url)
{
$content = file_get_content($url);
$content = str_replace('href="', 'href='proxy.php?url=', $content);
}

A very simple solution to the iframe problem. Solved?

Alexa ranking script

15
Sep
Posted at 5:26 pm in PHP, Programming

There are plenty of people out there searching for a script to get the alexa ranking for a site. Last time I looked I didn’t even find a free function to get the data, just some expensive solutions.

So anyway, here’s a PHP coded function we have made that collects the alexa-data with the SimpleXML function. It’s very easily coded so everyone should understand what it does. However, since it uses the SimpleXML function, it can only be used if you run PHP5. That’s a requirement.

Please give us some credit if you use the function. A comment or anything will work!

1
2
3
4
5
6
7
8
9
10
<?php
 
function getalexa($url){
// cdsrc.com
$request_url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=".$url;
$xml = simplexml_load_file($request_url) or die("feed not loading");
return $xml->SD->POPULARITY['TEXT'];
}
 
?>

And here’s a little description of how to use it:

11
12
13
14
<?php
echo getalexa("http://cdsrc.com");
// This would print the alexaranking of cdsrc.com
?>

Simple huh?

PHP SimpleXML function

11
Sep
Posted at 3:32 pm in PHP, Programming

SimpleXML intro:
PHP4 had one pretty big drawback when you had to use external libraries to parse XML files. PHP5 had a number of XML libraries that solve this problem and one of the easiest is SimpleXML.

To test and use the function, a XML document is needed.
In this post we will use our own sitemap, located here: http://cdsrc.com/sitemap.xml.

As we write this (the document will change in future since it’s a sitemap), the document looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://cdsrc.com/wp-content/plugins/google-sitemap-generator/sitemap.xsl"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/09/sitemap.xsd"	xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://cdsrc.com/</loc>
		<lastmod>2007-09-10T11:31:33+00:00</lastmod>
		<changefreq>daily</changefreq>
 
		<priority>1.0</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/programming/php-random-password-generator-function.html</loc>
		<lastmod>2007-09-10T11:31:33+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.3</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/programming/php-and-database-space-saving-tips.html</loc>
		<lastmod>2007-09-09T13:06:17+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.2</priority>
	</url>
 
	<url>
		<loc>http://cdsrc.com/linux/linuxbiggerthanwindows.html</loc>
		<lastmod>2007-09-09T12:45:00+00:00</lastmod>
		<changefreq>monthly</changefreq>
 
		<priority>0.3</priority>
	</url>
</urlset>

The documents structure is pretty easy to understand. All URL’s has a , , and . All this variables can now easily be accessed by the simple SimpleXML function.

There are a couple of methods you can use to load the XML file, one of them are to use file_get_contents() and another one, which we will use, will be shown here:

1
2
3
4
5
6
7
8
<?php
$url_location = 'http://cdsrc.com/sitemap.xml';
$sitemap = new SimpleXMLElement($url_location,null,true);
foreach($sitemap as $url) 
{
	echo "Url: {$url->loc}<br />Lastmod: {$url->lastmod}<br />Changefreq: {$url->changefreq}</br>Priority: {$url->priority}<br /><br />";
}
?>

There are no necessary comments to this as the code is pretty simple to understand. You access the “subfields” using $url->”subfield” and the counter goes through all the posts in the whole sitemap.

If we now would execute that script, we will get this output:

Url: http://cdsrc.com/
Lastmod: 2007-09-10T11:31:33+00:00
Changefreq: daily
Priority: 1.0
 
Url: http://cdsrc.com/programming/php-random-password-generator-function.html
Lastmod: 2007-09-10T11:31:33+00:00
Changefreq: monthly
Priority: 0.3
 
Url: http://cdsrc.com/programming/php-and-database-space-saving-tips.html
Lastmod: 2007-09-09T13:06:17+00:00
Changefreq: monthly
Priority: 0.2
 
Url: http://cdsrc.com/linux/linuxbiggerthanwindows.html
Lastmod: 2007-09-09T12:45:00+00:00
Changefreq: monthly
Priority: 0.3

Thats a very great and easy way of using the “new” SimpleXML PHP 5.0 function!

PHP Random password generator function

10
Sep
Posted at 11:25 am in PHP, Programming

Here’s a random password and string generator function for PHP.
It is totally free of use.

randpass(); Will give you a 8 characters long randomized string with numbers and letters.
The function also takes these arguments:
$numchars <-- The length in characters for the returned string.
$digits <------ Should the generated password contain figures? 1/0
$letters <----- Should it contain letters? 1/0
$sensitive <--- Both upper and lower letters? 1/0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function randpass($numchars=8,$digits=1,$letters=1,$sensitive=0)
{
	$dig = "12345678901234567890";
	$abc = "abcdefghijklmnopqrstuvwxyz";
	if($letters == 1)
	{
	    $str .= $abc;
	}
	if($sensitive == 1)
	{
	    $str .= strtoupper($abc);
	}
	if($digits == 1)
	{
		$str .= $dig;
	}
	for($i=0; $i < $numchars; $i++)
	{
		$randomized .= $str{rand() % strlen($str)};
	}
 
return $randomized;
}

To give an example of the code:

24
25
echo "Generated password: \n";
echo randpass(12,1,1,1);

The code above will result in:

Generated password:
4C8cly61h907