Category Archives: PHP

PHP US State Dropdown Array

Complete PHP State Dropdown Array Form
<!DOCTYPE html>
<html>
<head>
<title>States Dropdown Array</title>
<style type="text/css">
<!--
.states {
display:none;
}
-->
</style>
</head>
<body>
<?php
$states = array(
'AL' => "Alabama",
'AK' => "Alaska",
'AZ' => "Arizona",
'AR' => "Arkansas",
'CA' => "California",
'CO' => "Colorado",
'CT' => "Connecticut",
'DE' => "Delaware",
'DC' => "District Of Columbia",
'FL' => "Florida",
'GA' => "Georgia",
'HI' => "Hawaii",
'ID' => "Idaho",
'IL' => "Illinois",
'IN' => "Indiana",
'IA' => "Iowa",
'KS' => "Kansas",
'KY' => "Kentucky",
'LA' => "Louisiana",
'ME' => "Maine",
'MD' => "Maryland",
'MA' => "Massachusetts",
'MI' => "Michigan",
'MN' => "Minnesota",
'MS' => "Mississippi",
'MO' => "Missouri",
'MT' => "Montana",
'NE' => "Nebraska",
'NV' => "Nevada",
'NH' => "New Hampshire",
'NJ' => "New Jersey",
'NM' => "New Mexico",
'NY' => "New York",
'NC' => "North Carolina",
'ND' => "North Dakota",
'OH' => "Ohio",
'OK' => "Oklahoma",
'OR' => "Oregon",
'PA' => "Pennsylvania",
'RI' => "Rhode Island",
'SC' => "South Carolina",
'SD' => "South Dakota",
'TN' => "Tennessee",
'TX' => "Texas",
'UT' => "Utah",
'VT' => "Vermont",
'VA' => "Virginia",
'WA' => "Washington",
'WV' => "West Virginia",
'WI' => "Wisconsin",
'WY' => "Wyoming"
);
?>
<form>
<select name="state">
<option selected="selected" class="states">Select State</option>
<?php foreach ($states as $abbr => $state) {?>
<option value="<?php echo $abbr;?>"><?php echo $state;?></option>
<?php }?>
</select>
</form>
</body>
</html>

PHP Automatic Logout – Auto Logout

This PHP function automatically logs out a user after a specified amount of inactivity.
Usage: Add this PHP code to the top of your pages
<?php
session_start();
$inactive = 30; // Set timeout minutes
$logout_redirect_url = "index.php"; // Set logout URL
auto_logout($inactive, $logout_redirect_url);
function auto_logout($inactive, $logout_redirect_url)
{
global $inactive, $logout_redirect_url;
$inactive = $inactive * 60; // Converts minutes to seconds
if (isset($_SESSION['start_time']))
{
$elapsed_time = time() - $_SESSION['start_time'];
if ($elapsed_time >= $inactive)
{
session_destroy();
header("Location: $logout_redirect_url");
}
}
$_SESSION['start_time'] = time();
}

Php Insert Form Array Into Mysql With Pdo

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("INSERT INTO mytable (field1, field2) VALUES (?,?)");
for ($i = 0; $i < count($_POST['field1']); $i++)
{
$stmt->execute(array(
$_POST['field1'][$i],
$_POST['field2'][$i]
));
}
}
?>
<form method="post">
<b>field1 1</b><br>
<label>field1 <input type="text" name="field1[]"></label>
<br>
<label>field2 <input type="text" name="field2[]"></label>
<br>
<b>field1 2</b><br>
<label>field1 <input type="text" name="field1[]"></label>
<br>
<label>field2 <input type="text" name="field2[]"></label>
<input name="" type="submit" value="Submit">
</form>
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`field1` varchar(255) DEFAULT NULL,
`field2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;