Top
Home
######################### Show Page Source of a file #########################

<?php
show_source("thanks.php");
?>

######################### Echo Session Variables set #########################

<?php
session_start();
print_r($_SESSION);
?>

######################### Creates Unique ID with Extension #########################

<?php
echo uniqid(gfx414_,true);
?>

######################### Set Constant #########################

<?php
define("Website", "mooseloose.com");
echo Website;
?>  

######################### Function Syntax #########################

<?php
function writeMsg() {
   echo "Hello world!";
}

writeMsg(); // call the function
?>



######################### Handy Globals #########################

<?php
echo "<br>";
echo $_SERVER['SERVER_ADDR'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['REQUEST_TIME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['REMOTE_ADDR'];
echo "<br>";
echo $_SERVER['SCRIPT_FILENAME'];
?>

######################### Post to self and Verify #########################

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
First Name: <input type="text" name="fname">
Last Name: <input type="text" name="Last">
 <input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = $_REQUEST['fname'];
   if (empty($name)) {
       echo "Name is empty";
   } else {
       echo $name;
   }
$Last = $_REQUEST['Last'];
   if (empty($Last)) {
       echo "Last is empty";
   } else {
       echo $Last;
   }
}
?>

######################### Test for hackablility #########################

/%22%3E%3Cscript%3Ealert('Visit Moose Loose')%3C/script%3E

######################### Security Cleaning Example #########################

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 $name = test_input($_POST["name"]);
 $email = test_input($_POST["email"]);
 $website = test_input($_POST["website"]);
 $comment = test_input($_POST["comment"]);
 $gender = test_input($_POST["gender"]);
}

function test_input($data) {
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);
 return $data;
}
?>



######################### Security Cleaning Example #########################

<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }
 
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
  }
   
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail:
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website:
<input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
<label>Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">

</form>



######################### Ongoing Copyright notice #########################

<?php
echo "<p>Copyright &copy; 1996-" . date("Y") . " MooseLoose.com</p>";
?>

######################### Edit or Delete #########################

<?php
$submit=$_POST['submit'];
$conn = mysql_connect("HOST", "USERNAME", "PASSWORD", "DATABASE");
if (!$conn)
{
echo ("Not Connected");
}
$result= mysql_select_db("DATABASE",$conn);
if ($submit=="edit")
{
$query = "update TABLE set
FIELD_ONE='$FIELD_ONE', FIELD_TWO='$FIELD_TWO', FIELD_THREE='$FIELD_THREE' where id='$id'";
$result = mysql_query($query);
echo "Record $id successfully edited!";
}
elseif ($submit=="delete")
{
$query = "delete from TABLE where id='$id' ";
$result = mysql_query($query);
echo "Record $id has been forever deleted";
}
else
echo "problems";
?>

######################### Variable conditional #########################

<?php if($VARIABLE){ ?> <?php } ?>