How To Know If CloudFlare Is Working

  1. Login to your Cloudflare console
  2. Click the gear on the url and set “Page Rules”
  3. Check “Forwarding”
  4. Url Patern: http://yoursite.com/cloudflare
  5. Destination: http://yoursite.com/ison
  6. Add Rule and wait a few minutes.
  7. Go to your url, and you should be redirected to your “ison” url.

 

Get a Server’s IP that is Always Changing

So, my server at home’s IP address is always changing. Sometimes it’s sitting there broadcasting my development server’s website to the world on an IP that I don’t know.

So, when this happens, I have to go get physical access and bring up canyouseeme.org or something like it.

There are a few solutions to this problem:

  • DynDNS – A good solution, but I hate that they will cancel your service when you don’t login.
  • Get a static IP from your ISP – Costs extra money
  • Somehow, get the server to broadcast it’s address to a static place

I went with the last option. You could do this in a multitude of ways. You could have your server send it’s address in an email to you periodically or on boot. You could FTP it to a free FTP server. You get the idea.

I did this:
* danfolkes.com : that keeps it’s IP. It has PHP.
* lab.danfolkes.com : that changes it’s IP.

I setup a cron job on lab.danfolkes.com to run every 8 hours and open a PHP page:

0 */8 * * * /usr/bin/curl "http://danfolkes.com/path/to/file.php"

The page on danfolkes.com will record the last 50 IPs sent to it in a logfile:

	// danfolkes.com/path/to/file.php:
	$file = 'log/file.txt';
	$max = 50;
	$content = "\n<br/>" . date("Y-m-d H:i:s") . " " . $_SERVER['REMOTE_ADDR'] . "";
 
	$filecontents = file_get_contents($file);
	$filecontents = $content . $filecontents;
	$filecontents = implode("\n<br/>", array_slice(explode("\n<br/>", $filecontents),0, $max));
	echo $filecontents;
 
	file_put_contents($file, $filecontents);

So, if my server’s IP ever changes, there will be a nice little record of the last known IPs. I can go into my DNS settings for lab.danfolkes.com and set them to the new IP.

<!-- Log File -->
<br/>2013-07-30 12:56:09 24.125.92.189
<br/>2013-07-30 04:56:10 24.125.92.189
<br/>2013-07-29 20:56:21 24.125.92.189
<br/>2013-07-29 20:56:19 24.125.92.189
<br/>2013-07-29 20:56:15 24.125.92.189
<br/>2013-07-29 20:56:12 24.125.92.189
<br/>2013-07-29 20:55:48 24.125.92.189
<br/>2013-07-29 20:55:32 24.125.92.189
<br/>2013-07-29 20:50:04 24.125.92.189

Moderating Comments on a Freehostia Chocolate Account (Free)

The problem: Freehostia Free Accounts block outside connections.  This means that Akismet can’t access it’s servers and you can’t have spam checking.  So, all of your comments are spam spam spam!

I tried to add a captcha, but they got around that.  I tried to add Disqus using the plugin, but that failed to install because of the account’s limitations.

But, I was able to add moderated comments to my freehostia site by manually installing Disqus.  This is very necessary because otherwise you get hit with a bunch of spammers.

Here is how you manually install Disqus on WordPress.

  1. Create New Disqus Account 
  2. Go to your dashboard
  3. Click on your site
  4. Go to Settings
  5. And then click Install
  6. Select Universal Code
  7. Copy that code
  8. In /wp-content/themes/THEMENAME/single.php Replace:
    <?php comments_template( '', true ); ?>

    with:

    echo '	
     
    YOUR COPIED CODE HERE.  
     
        But you must remember to ESCAPE the 
        single quotes with backslashes example: \'
     
    	';
  9. Save, Upload, then go to one of your posts. You should see Disqus loading at the bottom.
  10. Remember to setup your Disqus the way you want it. It’s a little restrictive out of the box. You can even add Akismet to Disqus in their admin interface.

Short Domain Name Finder

This is a short url finder I created.
It checks to see if the domain is available remembers.

Short Domain Name Finder

There is a python script in the background that populates a mysql database which displays it with php.

Sending Email with PHP, Gmail or Google Apps, Mail, and PEAR on Ubuntu

First Install Pear, Mail, Mail Mime, Net SMTP. Restart Apache:

sudo apt-get install php-pear
sudo pear install mail
sudo pear install mail_mime
sudo pear install Net_SMTP
sudo /etc/init.d/apache2 restart

Here is the PHP code I used to send an email:

try {
	require_once "Mail.php";
 
	$from = "[email protected]";
	$to = "{$email}";
	$subject = "Hi!";
	$body = "Hi,\n\nHow are you?";
 
	$host = "ssl://smtp.gmail.com";
	$port = "465";
	$username = "[email protected]"; //This can also be a Google Apps account
	$password = "PASSWORD";
 
	$headers = array ('From' => $from,
	  'To' => $to,
	  'Subject' => $subject);
	$smtp = Mail::factory('smtp',
	  array ('host' => $host,
		'port' => $port,
		'auth' => true,
		'username' => $username,
		'password' => $password));
 
	$mail = $smtp->send($to, $headers, $body);
 
	if (PEAR::isError($mail)) {
	  echo("<p>" . $mail->getMessage() . "</p>");
	 } else {
	  echo("<p>Message successfully sent!</p>");
	 }
} catch (Exception $e) {
	echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Web Service using JSONP from External Domain – PHP – JQuery

You may want to look at this previous post where I setup a PHP Webservice that uses JSON PDO – Accessed by JQuery AJAX

OK, so this code will use php to generate the JSON code that will get delivered over the internet to the JSONP (JSON Padding) client on an external domain. This allows your webservice to be available too whoever wants to call it.

Here is the PHP Code: (Not all of it, but enough to get the idea)

//http://SERVER_domain.com/webservice.php
private function GetTriviaTag($row) {
	$t = new Tag();
	$t->id = $row['id'];
	$t->triviaid = $row['triviaid'];
	$t->tagid = $row['tagid'];
	return $t;
}
public function GetWhere($where) {
	$retArray;
 
	$sql = "SELECT * FROM trivia_tags WHERE {$where}";
	foreach ($this->pdo->query($sql) as $row)
	{
		$retArray[] = $this->GetTriviaTag($row);
	}
	return $retArray;
}
$where = " (1 = 1)";  // THIS WILL SELECT ALL
$tarray = GetWhere($where);
 
//OUTPUT JSON:
header('Content-type: application/json');
echo $_GET['callback'];  // <-- THIS IS IMPORTANT FOR EXTERNAL DOMAIN ACCESS!!!
echo "(" . json_encode($tarray) . ")";

And now, to access it from javascript in an external domain (non – SERVER_domain.com ):
http://CLIENT_domain.com/Test.html:

<html>
	<head>
		<title>Test</title>
		<script src="http://code.jquery.com/jquery-latest.js"></script>
	</head>
	<body>
		<div id="triviaQuestions"> loading... </div>
		<script>
			$(function() {
				// THE 'callback=?' in this request is IMPORTANT FOR EXTERNAL DOMAIN ACCESS!!!
				$.getJSON('http://SERVER_domain.com/webservice.php&callback=?', function(data) {
					var items = [];
					var htmlResult = "";
					$.each(data, function(key, val) {
						htmlResult += "<br />Q: "+ val.id +", '"+ val.question +"";
					});
					$('#triviaQuestions').html(htmlResult);
				});
			});
		</script>
	</body>
</html>

PHP Webservice that uses JSON PDO – Accessed by JQuery AJAX

You may want to look at this previous post where I setup a Web Service using JSONP from External Domain – PHP – JQuery

The HTML that calls the Javascript:

<div id="taglist"></div>
<input name="searchTags" id="searchTags" type="text" />
<input type="button" name="searchtags" onclick="getTags()" value="search"/>
<div id="tagresult">
</div>

The JavaScript (JQuery) AJAX Request and Response Processing:

function getTags()
	{
		var q = $('#searchTags').val();
		$.getJSON('ws.php?type=tag&wN=name&wO=like&wV=' + q, function(data) {
			var items = [];
			var htmlResult = "";
			$.each(data, function(key, val) {
				//As you can see, you can access the JSON (at the bottom of the page) by doing val.PROPERTYNAME:
				htmlResult += "<br />Add : <a href=\"#\" onclick=\"addTag("+ val.id +", '"+ val.getfullname +"'); return false;\">"+ val.getfullname +"</a>";
			});
			$('#tagresult').html(htmlResult);
		});
	}

PHP Code that returns JSON in the PHP file called by the AJAX: (ws.php)

	$ta = new TagAccess();
	$tarray = $ta->GetTagsWhere(" {$whereName} LIKE '%{$whereVariable}%'");
	header('Content-type: application/json');
	echo "",  json_encode($tarray), "";

The GetTagsWhere Helper Function:

    public function GetTagsWhere($where) {
    	$retArray;
 
    	$sql = "SELECT * FROM tags WHERE {$where}";
    	// THE PDO Object was created previously
	foreach ($this->pdo->query($sql) as $row)
	{
		$retArray[] = $this->GetTag($row);
	}
	return $retArray;
    }

JSON Returned:

[{"id":"4","name":"FICTION","parentid":"3","getfullname":"BOOKS > FICTION"},{"id":"5","name":"NON-FICTION","parentid":"3","getfullname":"BOOKS > NON-FICTION"}]

Simple PHP Pseudo Proxy

php proxy simple pseudo

This is the code for a simple php pseudo proxy. If you are blocked by a firewall and google cache isn’t working for some reason. You can use a url with this php file in it:

<?php
$filename = $_GET["url"];
if (isset($filename))
{
        $handle = fopen($filename, "rb");
        $contents = stream_get_contents($handle);
        print $contents;
        fclose($handle);
}
else
{
        echo "url";
?>
<form method="GET">
<input type="text" name="url" /><input type="submit"/>
</form>
<?php
}
?>

Then you can go to where the url is hosted and try something like this:

index.php?url=http%3A%2F%2Fwww.last.fm%2Fuser%2Fdanfolkes%2Fcharts%3Frangetype%3D6month%26subtype%3Dartists

Facebook Website Integration – HTML


To integrate Facebook on to a website without the use of a facebook app you need these things:

Once you have both of those things you can do this in feedburner:

  • Add a new feed (your facebook url)
  • Go to the publicize tab
  • Go to BuzzBoost
  • Sroll down and fill out the form, you will be given html code.
  • <script src="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates?format=sigpro" type="text/javascript" ></script>
    <noscript><p>Subscribe to RSS headline updates from: 
    <a href="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates"></a>
    <br/>Powered by FeedBurner</p> 
    </noscript>
  • Preview:

And that’s it!
You can now style it up a little bit with some CSS and stuff.

PHP API – Google Calendar – Updating Visibility

The Privacy / Visibility Part:

$event->visibility->value = "http://schemas.google.com/g/2005#event.private";

Here it is in context: Continue reading “PHP API – Google Calendar – Updating Visibility”