Codigo Ficheros PHP
Codigo Ficheros PHP
freight.html
<html>
<body>
<table border="0" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC" align="center">Distance</td>
<td bgcolor="#CCCCCC" align="center">Cost</td>
</tr>
<tr>
<td align="right">50</td>
<td align="right">5</td>
</tr>
<tr>
<td align="right">100</td>
<td align="right">10</td>
</tr>
<tr>
<td align="right">150</td>
<td align="right">15</td>
</tr>
<tr>
<td align="right">200</td>
<td align="right">20</td>
</tr>
<tr>
<td align="right">250</td>
<td align="right">25</td>
</tr>
</table>
</body>
</html>
freight.php
<html>
<body>
<table border = 0 cellpadding = 3>
<tr>
<td bgcolor = "#CCCCCC" align = center>Distance</td>
<td bgcolor = "#CCCCCC" align = center>Cost</td>
</tr>
<?php
$distance = 50;
while ($distance <= 250 ){
echo "<tr>\n <td align = right>$distance</td>\n";
echo " <td align = right>". $distance / 10 ."</td>\n</tr>\n";
$distance += 50;
} ?>
</table>
</body>
</html>
orderform.html
<html>
<body>
<form action="processorder.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
<td width="150">Item</td>
<td width="15">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>How did you find Bob's?</td>
<td><select name="find">
<option value = "a">I'm a regular customer</option>
<option value = "b">TV advertising</option>
<option value = "c">Phone directory</option>
<option value = "d">Word of mouth</option>
</select> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit
Order"></td>
</tr>
</table>
</form>
</body>
</html>
processorder.php
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$find = $_POST['find'];
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo '<p>Order processed at ';
echo date('H:i, jS F');
echo '</p>';
echo '<p>Your order is as follows: </p>';
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';
if( $totalqty == 0) {
echo 'You did not order anything on the previous page!<br />';
}
else {
if ( $tireqty > 0 )
echo $tireqty.' tires<br />';
if ( $oilqty > 0 )
echo $oilqty.' bottles of oil<br />';
if ( $sparkqty > 0 )
echo $sparkqty.' spark plugs<br />';
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty *
SPARKPRICE;
echo 'Subtotal: $'.number_format($totalamount,2).'<br />';
$taxrate = 0.10; // local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
echo 'Total including tax: $'.number_format($totalamount,2).'<br />';
/*
if($find == 'a')
echo '<p>Regular customer.</p>';
elseif($find == 'b')
echo '<p>Customer referred by TV advert.</p>';
elseif($find == 'c')
echo '<p>Customer referred by phone directory.</p>';
elseif($find == 'd')
echo '<p>Customer referred by word of mouth.</p>';
else
echo '<p>We do not know how this customer found us.</p>';
*/
switch ($find){
case a:
echo '<p>Regular customer.</p>';
break;
case b:
echo '<p>Customer referred by TV advert.</p>';
break;
case c:
echo '<p>Customer referred by phone directory.</p>';
break;
case d:
echo '<p>Customer referred by word of mouth.</p>';
break;
default:
echo '<p>We do not know how this customer found us.</p>';
break;
}
?>
</body>
</html>
orderform.html
<html>
<head>
<title>Bob's Auto Parts</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Form</h2>
<form action="processorder.php" method=post>
<table border=0>
<tr bgcolor=#cccccc>
<td width=150>Item</td>
<td width=15>Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="left"><input type="text" name="tireqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Oil</td>
<td align="left"><input type="text" name="oilqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="left"><input type="text" name="sparkqty" size="3"
maxlength="3"></td>
</tr>
<tr>
<td>Shipping Address</td>
<td align="center"><input type="text" name="address" size="40"
maxlength="40"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Submit Order"></td>
</tr>
</table>
</form>
</body>
</html>
processorder.php
<?php
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
$date = date('H:i, jS F');
echo '<p>Order processed at '. $date . '</p>';
echo '<p>Your order is as follows: </p>';
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '. $totalqty .'<br />';
if( $totalqty == 0){
echo 'You did not order anything on the previous page!<br />';
}
else {
if ( $tireqty>0 )
echo $tireqty.' tires<br />';
if ( $oilqty>0 )
echo $oilqty.' bottles of oil<br />';
if ( $sparkqty>0 )
echo $sparkqty.' spark plugs<br />';
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty *
SPARKPRICE;
$totalamount=number_format($totalamount, 2, '.', ' ');
echo '<p>Total of order is '.$totalamount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
.$sparkqty." spark plugs\t\$".$totalamount ."\t". $address."\n";
@$fp = fopen("$DOCUMENT_ROOT/ejemplos/02/orders/orders.txt", 'ab');
flock($fp, LOCK_EX);
if (!$fp) {
echo '<p><strong> Your order could not be processed at this time. '
.'Please try again later.</strong></p></body></html>';
exit;
}
</head>
<body>
<center>
<h1>Bob's Auto Parts</h1>
<table width = 100%>
<tr>
<?php
for ( $i = 0; $i < 3; $i++ ) {
echo '<td align="center"><img src="';
echo $pictures[$i];
echo '"width="100" height="100"></td>';
}
?>
</tr>
</table> </center>
</body>
</html>
vieworders.php
<?php
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$orders= file("$DOCUMENT_ROOT/ejemplos/02/orders/orders.txt");
$number_of_orders = count($orders);
if ($number_of_orders == 0) {
echo '<p><strong>No orders pending . Please try again
later.</strong></p>';
}
for ($i= 0; $i< $number_of_orders; $i++) {
echo $orders[$i].'<br />';
}
?>
vieworders2.php
<?php $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; ?>
<html>
<head> <title>Bob's Auto Parts - Customer Orders</title> </head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Customer Orders</h2>
<?php
// Read in the entire file. Each order becomes an element in the array
$orders= file("$DOCUMENT_ROOT/ejemplos/02/orders/orders.txt");
$number_of_orders = count($orders);
if ($number_of_orders == 0) {
echo '<p><strong>No orders pending . Please try again
later.</strong></p>';
}
echo "<table border=1>\n";
echo '<tr><th bgcolor="#CCCCFF">Order Date</th>
<th bgcolor="#CCCCFF">Tires</th>
<th bgcolor="#CCCCFF">Oil</th>
<th bgcolor="#CCCCFF">Spark Plugs</th>
<th bgcolor="#CCCCFF">Total</th>
<th bgcolor="#CCCCFF">Address</th>
<tr>';
<h1>Feedback submitted</h1>
<p>Your feedback has been sent.</p>
</body></html>
home.html
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style type="text/css">
h1 {color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif}
.menu {color:white; font-size:12pt; text-align:center; font-family:arial,sansserif; font-weight:bold}
td {background:black}
p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif}
p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sansserif; fontweight:bold}
a:link,a:visited,a:active {color:white}
</style>
</head>
<body>
<!-- page header -->
<table width="100%" cellpadding="12" cellspacing="0" border="0">
<tr bgcolor="black">
<td align="left"><img src="logo.gif" alt="TLA logo" height=70
width=70></td>
<td>
<h1>TLA Consulting</h1> </td>
<td align="right"><img src="logo.gif" alt="TLA logo" height=70
width=70></td>
</tr>
</table>
<!-- menu -->
<table width="100%" bgcolor="white" cellpadding="4" cellspacing="4">
<tr >
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Home</span></td>
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Contact</span></td>
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Services</span></td>
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Site Map</span></td>
</tr>
</table>
<!-- page content -->
<p>Welcome to the home of TLA Consulting. Please take some time to get to
know us.</p>
<p>We specialize in serving your business needs and hope to hear from you
soon.</p>
<!-- page footer -->
<table width="100%" bgcolor="black" cellpadding="12" border="0">
<tr>
<td>
<p class="foot">© TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our <a href="legal.php">legal information
page</a></p>
</td>
</tr>
</table>
</body>
</html>
header.inc
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style type="text/css">
h1 {color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif}
.menu {color:white; font-size:12pt; text-align:center; font-family:arial,sansserif; fontweight:bold}
td {background:black}
p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif}
p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sansserif; fontweight:bold}
a:link,a:visited,a:active {color:white}
</style>
</head>
<body>
<!-- page header -->
<table width="100%" cellpadding="12" cellspacing="0" border="0">
<tr bgcolor="black">
<td align="left"><img src="logo.gif" alt="TLA logo" height=70
width=70></td>
<td>
<h1>TLA Consulting</h1>
</td>
<td align="right"><img src="logo.gif" alt="TLA logo" height=70
width=70></td>
</tr>
</table>
<!-- menu -->
<table width="100%" bgcolor="white" cellpadding="4" cellspacing="4">
<tr >
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Home</span></td>
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Contact</span></td>
<td width="25%">
<img src="s-logo.gif" alt="" height=20 width=20> <span
class="menu">Services</span></td>
<td width="25%">
?>
var_args.php
<?php
function var_args() {
echo "Number of parameters:";
echo func_num_args(); // recoge el nmero de argumentos
echo '<br />';
$args = func_get_args();
foreach ($args as $arg)
echo $arg.'<br />';
}
var_args(1,2,3);
var_args("hello", 47.3);
?>
page.inc
<?php
class Page {
public $content;
public $title = 'TLA Consulting Pty Ltd';
public $keywords = 'TLA Consulting, Three Letter Abbreviation, some of my
best friends are
search engines';
public $buttons = array( 'Home' => 'home.php', 'Contact' =>
'contact.php',
'Services' => 'services.php', 'Site Map' => 'map.php' );
public function __set($name, $value) {
$this->$name = $value;
}
public function Display( ) {
echo "<html>\n<head>\n";
$width = 100/count($buttons);
//calculate button size
while (list($name, $url) = each($buttons)) {
$this -> DisplayButton($width, $name, $url, !$this>IsURLCurrentPage($url));
}
echo " </tr>\n";
echo "</table>\n";
}
public function IsURLCurrentPage($url) {
if(strpos($_SERVER['PHP_SELF'], $url )==false) {
return false;
} else {
return true;
}
}
public function DisplayButton($width, $name, $url, $active = true) {
if ($active) {
echo "<td width ='$width%'>
<a href ='$url'>
<img src ='s-logo.gif' alt ='$name' border ='0' /></a>
<a href ='$url'><span class='menu'>$name</span></a></td>";
}
else
{
echo "<td width ='$width%'>
<img src ='side-logo.gif'>
<span class='menu'>$name</span></td>";
}
}
public function DisplayFooter() {
?>
<table width = "100%" bgcolor ="black" cellpadding ="12" border ="0">
<tr>
<td>
<p class="foot">© TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our <a href ="">legal information
page</a></p>
</td>
</tr>
</table>
<?php
}
}
?>
home.php
<?php
require ('page.inc');
$homepage = new Page();
$homepage->content =' <p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs and hope to hear
from you soon.</p>';
$homepage->Display();
?>
services.php
<?php
require ('page.inc');
class ServicesPage extends Page {
private $row2buttons = array( 'Re-engineering' => 'reengineering.php',
'Standards Compliance' => 'standards.php', 'Buzzword Compliance' =>
'buzzword.php',
'Mission Statements' => 'mission.php' );
public function Display() {
echo "<html>\n<head>\n";
$this -> DisplayTitle();
$this -> DisplayKeywords();
$this -> DisplayStyles();
echo "</head>\n<body>\n";
$this -> DisplayHeader();
$this -> DisplayMenu($this->buttons);
$this -> DisplayMenu($this->row2buttons);
echo $this->content;
$this -> DisplayFooter();
echo "</body>\n</html>\n";
}
}
$services = new ServicesPage();
$services -> content ='<p>At TLA Consulting, we offer a number of services.
Perhaps the productivity of your employees would improve if we reengineered your business.
Maybe all your business needs is a fresh mission statement, or a new
batch of
buzzwords.</p>';
$services -> Display();
?>
reflection.php
<?php
require_once('page.inc');
$class = new ReflectionClass('Page');
echo '<pre>'. $class . '</pre>';
?>
iterator.php
<?php
class ObjectIterator implements Iterator {
private $obj;
private $count;
private $currentIndex;
function __construct($obj) {
$this->obj = $obj;
$this->count = count($this->obj->data);
}
function rewind() {
$this->currentIndex = 0;
}
function valid() {
return $this->currentIndex < $this->count;
}
function key() {
return $this->currentIndex;
}
function current() {
return $this->obj->data[$this->currentIndex];
}
function next() {
$this->currentIndex++;
}
}
class Object implements IteratorAggregate {
public $data = array();
function __construct($in) {
$this->data = $in;
}
function getIterator() {
return new ObjectIterator($this);
}
}
$myObject = new Object(array(2, 4, 6, 8, 10));
$myIterator = $myObject->getIterator();
for($myIterator->rewind(); $myIterator->valid(); $myIterator->next()) {
$key = $myIterator->key();
$value = $myIterator->current();
echo "$key => $value <br />";
}
?>
printable.php
<?php
class MyObject {
protected $name = 'Antonio';
public function __toString() {
return "My name is: {$this->name}\n";
}
}
$obj = new MyObject;
echo $obj;
echo '<br/>';
echo serialize($obj);
?>
Otra versin:
<?php
class Printable{
var $prueba1= 'Hola';
var $prueba2;
var $a = array (1, 2, array ("a", "b", "c"));
public function _toString(){
return var_export($this, true);
}
}
$p= new Printable;
echo serialize($p);
echo '<br/>';
echo var_dump($p);
echo '<br/>';
echo $p->_toString();
?>
file_exception.php
?php
class fileOpenException extends Exception {
function __toString() {
return 'fileOpenException '. $this->getCode() . ': '. $this>getMessage().'<br />'.' in '
. $this->getFile(). ' on line '. $this->getLine()
. '<br />';
}
}
class fileWriteException extends Exception {
function __toString() {
return 'fileWriteException '. $this->getCode() . ': '. $this>getMessage().'<br />'.' in '
. $this->getFile(). ' on line '. $this->getLine() . '<br />';
}
}
class fileLockException extends Exception {
function __toString() {
return 'fileLockException '. $this->getCode() . ': '. $this>getMessage().'<br />'.' in '
. $this->getFile(). ' on line '. $this->getLine() . '<br />';
}
}
?>
user_defined_exception.php
<?php
class myException extends Exception {
function __toString() {
return '<table border><tr><td><strong>Exception '. $this->getCode()
</body>
</html>
processorder.php
<?php
require_once('file_exceptions.php');
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
$date = date('H:i, jS F');
echo '<p>Order processed at '. $date. '</p>';
echo '<p>Your order is as follows: </p>';
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';
if( $totalqty == 0) {
echo 'You did not order anything on the previous page!<br />';
}
else {
if ( $tireqty>0 )
echo $tireqty.' tires<br />';
if ( $oilqty>0 )
echo $oilqty.' bottles of oil<br />';
if ( $sparkqty>0 )
echo $sparkqty.' spark plugs<br />';
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount= $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty *
SPARKPRICE;
$totalamount= number_format($totalamount, 2, '.', ' ');
echo '<p>Total of order is '.$totalamount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
.$sparkqty." spark plugs\t\$".$totalamount ."\t". $address."\n";
// open file for appending
try {
if (!($fp = @fopen("$DOCUMENT_ROOT/ejemplos/02/orders/orders.txt",
'ab')))
throw new fileOpenException();
if (!flock($fp, LOCK_EX))
throw new fileLockException();
if (!fwrite($fp, $outputstring, strlen($outputstring)))
throw new fileWriteException();
flock($fp, LOCK_UN);
fclose($fp);
echo '<p>Order written.</p>';
}
catch (fileOpenException $foe) {
echo '<p><strong>Orders file could not be opened. '
.'Please contact our webmaster for help.</strong></p>';
}
catch (Exception $e) {
echo '<p><strong>Your order could not be processed at this time. '
.'Please try again later.</strong></p>';
}
?>
</body>
</html>
TRABAJAR CON MySQL
Abrir Simbolo del Sistema (Consola de comandos) e ir a la ubicacin de mysql:
cd xampp/mysql/bin/mysql -u root -h localhost
MariaDB [none]> update user set password= 18061962 where user=root;
MariaDB [none]> create database books;
MariaDB [none]> create user manuel@localhost identified by 18061962;
MariaDB [none]> grant all privileges on books.* to manuel identified by
18061962;
MariaDB [none]> use books;
MariaDB [books]> create table customers ( customerid int unsigned not null
auto_increment primary key, name char(50) not null, address char(100) not
null, city char(30) not null );
MariaDB [books]> create table orders ( orderid int unsigned not null
auto_increment primary key,
customerid int unsigned not null, amount float(6,2), date date not null );
MariaDB [books]> create table books ( isbn char(13) not null primary key,
author char(50),
title char(100), price float(4,2) );
MariaDB [books]> create table order_items ( orderid int unsigned not null, isbn
char(13) not null,
quantity tinyint unsigned, primary key (orderid, isbn) );
MariaDB [books]> alter table order_items add foreign key (orderid) references
orders (orderid);
MariaDB [books]> create table book_reviews ( isbn char(13) not null primary
key,
review text);
MariaDB [books]> show databases;
MariaDB [books]> show tables;
MariaDB [books]> describe customers;
MariaDB [books]> source C:\xampp\htdocs\ejemplos\10\book_insert.sql
insert into customers values
(NULL, "Julie Smith", "25 Oak Street", "Airport West"),
(NULL, "Alan Wong", "1/47 Haines Avenue", "Box Hill"),
(NULL, "Michelle Arthur", "357 North Road", "Yarraville");
search.html
<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head>
<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select> <br />
Enter Search Term:<br />
<input name="searchterm" type="text"> <br />
<input type="submit" value="Search">
</form>
</body>
</html>
results.php
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
// create short variable names
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm) {
echo 'You have not entered search details. Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
@ $db = new mysqli('localhost', 'manuel', '18061962', 'books');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from books where ".$searchtype." like '%".
$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of books found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
$result->free(); // tambin: mysqli_free_result($result)
$db->close();
// mysqli_close($db)
?>
</body>
</html>
newbook.html
<html>
<head>
$title = addslashes($title);
$price = doubleval($price);
}
@ $db = new mysqli('localhost', 'manuel', '18061962', 'books');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "insert into books values ('".$isbn."', '".$author."', '".$title."', '".
$price."')";
$result = $db->query($query);
if ($result)
echo $db->affected_rows.' book inserted into database.';
$db->close();
?>
</body>
</html>
search_generic.html
<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head>
<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="results_generic.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author
<option value="title">Title
<option value="isbn">ISBN
</select> <br />
Enter Search Term:<br />
<input name="searchterm" type=text> <br />
<input type=submit value="Search">
</form>
</body>
</html>
results_generic.php
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm){
echo 'You have not entered search details. Please go back and try
again.';
exit;
}
if (!get_magic_quotes_gpc()){
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
// set up for using PEAR DB
require_once('DB.php');
$user = 'manuel';
$pass = '18061962';
$host = 'localhost';
$db_name = 'books';
// set up universal connection string or DSN
$dsn = "mysqli://$user:$pass@$host/$db_name";
// connect to database
$db = &DB::connect($dsn);
// check if connection worked
if (DB::isError($db)){
echo $db->getMessage();
exit;
}
// perform query
$query = "select * from books where ".$searchtype." like '%".
$searchterm."%'";
$result = $db->query($query);
// check that result was ok
if (DB::isError($result)){
echo $db->getMessage();
exit;
}
// get number of returned rows
$num_results = $result->numRows();
// display each returned row
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
echo '<p><strong>'.($i+1).'. Title: ';
echo htmlspecialchars(stripslashes($row['title']));
echo '</strong><br />Author: ';
echo stripslashes($row['author']);
echo '<br />ISBN: ';
echo stripslashes($row['isbn']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '</p>';
}
// disconnect from database
$db->disconnect();
?>
</body>
</html>
basic_function.sql
//
delimiter ;
secret.php
<?php
@ $name = $_POST['name'];
@ $password = $_POST['password'];
if(empty($name)||empty($password)) {
//Visitor needs to enter a name and password
?>
<h1>Please Log In</h1>
This page is secret.
<form method="post" action="secret.php">
<table border="1">
<tr>
<th> Username </th>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<th> Password </th>
<td> <input type="password" name="password"> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Log In">
</td>
</tr>
</table>
</form>
<?php
}
else if($name=='user'&&$password=='pass') {
// visitor's name and password combination are correct
echo '<h1>Here it is!</h1>';
echo 'I bet you are glad you can see this secret page.';
}
else {
// visitor's name and password combination are not correct
echo '<h1>Go Away!</h1>';
echo 'You are not authorized to view this resource.';
}
?>
secretdb.php
<?php
$name = $_POST['name'];
$password = $_POST['password'];
if(!isset($_POST['name'])&&!isset($_POST['password'])) {
//Visitor needs to enter a name and password
?>
<h1>Please Log In</h1>
This page is secret.
}
?>
createauthdb.sql
create database auth;
use auth;
create table authorised_users ( name varchar(20), password varchar(40),
primary key (name) );
insert into authorised_users values ( 'username', 'password' );
insert into authorised_users values ( 'testuser', sha1('password') );
grant select on auth.* to 'webauth' identified by 'passw';
flush privileges;
MariaDB> source C:/xampp/htdocs/ejemplos/16/createauthdb.sql;
http.php
<?php
// if we are using IIS, we need to set $PHP_AUTH_USER and $PHP_AUTH_PW
if (substr($SERVER_SOFTWARE, 0, 9) == 'Microsoft' && !
isset($PHP_AUTH_USER) &&
!isset($PHP_AUTH_PW) && substr($HTTP_AUTHORIZATION, 0, 6) == 'Basic ' )
{
list($PHP_AUTH_USER, $PHP_AUTH_PW) =
explode(':', base64_decode(substr($HTTP_AUTHORIZATION, 6)));
}
// Replace this if statement with a database query or similar
if ($PHP_AUTH_USER != 'user' || $PHP_AUTH_PW != 'pass'){
// visitor has not yet given details, or their name and password combination
are not correct
header('WWW-Authenticate: Basic realm="Realm-Name"');
if (substr($SERVER_SOFTWARE, 0, 9) == 'Microsoft')
header('Status: 401 Unauthorized');
else
header('HTTP/1.0 401 Unauthorized');
echo '<h1>Go Away!</h1>';
echo 'You are not authorized to view this resource.';
}
else{ // visitor has provided correct details
echo '<h1>Here it is!</h1>';
echo '<p>I bet you are glad you can see this secret page.</p>';
}
?>
private_mail.php
<html>
<body>
<h1>Send Me Private Mail</h1>
<?php
// you might need to change this line, if you do not use
// the default ports, 80 for normal traffic and 443 for SSL
if($_SERVER['SERVER_PORT']!=443)
echo '<p><font color = red>WARNING: you have not connected to this
page using SSL.
Your message could be read by others.</font></p>';
?>