PHP and MySQL Tips

Remember that PHP code is interpreted before going to the browser.

Compare the two code sections:

Before the page renders

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p> This is regular HTML</p>
<?php echo '<p>This is HTML via <strong>PHP!!</strong></p>';

//this is a comment. It is NOT seen at all

?>

</body>
</html>

After the page renders

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p> This is regular HTML at wicketbang.com</p>
<p>This is HTML via <strong>PHP!!</strong> at wicketbang.com</p>
</body>
</html>

 

What this means is that you can run ANY server side code and it will be hidden from the user.

A good example of this is found at the password protect page sample.

Here is the code for it:

Before:

<html>
<head>
<title>PHP Password Protect Test</title>
</head>

<?php
// Define your username and password
$username = "someuser";
$password = "somepassword";

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<body>

<h1 align="center">Who are YOU?</font></h1>
<p>UN: someuser</p>
<p>PW: somepassword</p>
<CENTER>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><font color="#FFFFFF"><label for="txtUsername">Username:</label>
<br /><input type="text" title="Enter your Username" name="txtUsername" /></p>
<p><font color="#FFFFFF"><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 {
?>

<div align="center">Almost <a href="secret.htm">there
</a> </font>
<?php
}
?>
</div>

</body>
</html>

and After:

<html>
<head>
<title>PHP Password Protect Test</title>
</head>

<body>

<h1 align="center">Who are YOU?</font></h1>
<p>UN: someuser</p>
<p>PW: somepassword</p>
<CENTER>

<form name="form" method="post" action="/test/password.php">
<p><font color="#FFFFFF"><label for="txtUsername">Username:</label>
<br /><input type="text" title="Enter your Username" name="txtUsername" /></p>
<p><font color="#FFFFFF"><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>
</div>

</body>
</html>

Notice that there is NOTHING in between the head and the body in the rendered page. This was where we stuck the actual code and the password. It has been bolded for you in the before section.

Sample PHP Scripts

A sample DB entry form...


Here's a sample of a PHP application that tells you how much money you have, after allowing for bills that haven't come in yet, as well as debits and deposits that haven't cleared yet. It does NOT tie into a MySQL database. It simply calculates the Me MONEY! ( or...how much do you have to blow on junk, while still being a responsible adult ;-) )


Here are some tutorials that I found interesting

PHP Image Rotation

This is a tutorial on a rotating image php script that occurs when the user refreshs the page. It can be found at http://www.alistapart.com/articles/randomizer/

Add a meta tag to auto refresh the page and you're in business! For an example of this in action, visit http://www.greymatterideas.com/music.htm


 

Back to Class Tips

Back to Home