How to Tweet your Knol Updates (Page 4)

A Tutorial

Wouldn't it be super, if after you update a knol article, you could tweet your followers about it? Well now you can.

Authors

Written 2010 by Will Johnson Jr, you can email me at fft2001@aol.com
or post your comments for public view far below.
Creative Commons Attribution 3.0 License

<-- Back to How to Tweet your Knol Updates (Page 3)
--> Forward to How to Tweet your Knol Updates (Page 5)


Okay there's just two more things we have to cover before we show the whole project.  This Tweet button is for you to use, to tell your followers that you've just updated an article here at Knol.  We don't want casual readers to come by and click the button!  You'd get a lot of duplicates just because some reader is trying something out.

How do we prevent that?  We can't prevent them from clicking the button, but we can prevent it from updating Twitter.  We do that by putting a password on the PHP script.


Here is the code:
<?php
// Define your password
$password = "somepassword";
if ($_POST['txtPassword'] != $password) {
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtpassword">Password:</label><br />
    <input type="password" title="Enter your password" name="txtPassword" /></p>
    <p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?php
}
else {
?>
<p>This is the protected page. Your private content goes here.</p>
<?php
}
?>

What this script does, is each time the user enters an incorrect password, it simply asks again, or they can just close the window.  If they enter the correct password, it will then display that text "This is the protected page..."

For our purposes, what we're going to do, instead of just displaying some text, we're going to use that logic, to continue on to the rest of our PHP script!  Clever isn't it!

So part of that "rest" is going to be the previous instruction we built:
$twitter.data = "status=I just updated my article <a href = \"".getenv("HTTP_REFERER")."\">How to Tweet your Knol Updates</a>";

But wait... I'm passing in the string "How to Tweet your Knol Updates" in the URL.  How do I get it out of the URL and be able to use it as an argument to pass to Twitter ?  We do that using the $_GET function.  Now all the guidebooks will tell you that this is used to collect data from a form passed in using the Get method.  Don't tell them that we've figured out an extra way to use it, that doesn't require forms at all.  They will be very jealous.

So remembering that in the URL we called this argument the Title our value now becomes:
$twitter.data = "status=I just updated my article <a href = \"".getenv("HTTP_REFERER")."\">".$_GET["Title"]."</a>";

We also had the argument Prefix, which you can use if you like or discard if you like.  If you use it, you can pass it in, or hardcode it directly into the PHP.  If you decide that you want to pass it in, then our value now becomes:
$twitter_data = "status=".$_GET["Prefix"]." <a href = \"".getenv("HTTP_REFERER")."\">".$_GET["Title"]."</a>";


The parser probably doesn't like all those quote marks, so let's code it like this instead:
$ref_url = getenv("HTTP_REFERER");
$prefix = $_GET["Prefix"];
$title = $_GET["Title"];
$twitter_data = "status=".$prefix." <a href = \"".$ref_url."\">".$title."</a>";

That's better.

Are you totally confused yet?  The "." symbol by the way, as you probably already guessed, is used to join together two string values.  A string is a set of characters.  Let's put together what we've learned so far, except for the final step of actually updating it to Twitter.  Click the birdy!



Poop!  It doesn't quite work does it yet.  I'll have to think about it a bit more.

Since our code on Page 2 of this article worked, it must be that that password-getting code is screwing us up.  I lifted that off the Internet !  That's what you get!  I think what must be happening is due to this code being  recursive, i.e. it calls itself.  It must be calling itself at least once and thereby obscuring the original URL Referent.  The question remains, how do we fix that?

<-- Back to How to Tweet your Knol Updates (Page 3)
--> Forward to How to Tweet your Knol Updates (Page 5)