Name: Anonymous 2010-06-01 2:49
I had no idea I had a course in PHP this semester, evidently I do and the exam is tomorrow. I need to learn PHP in 18 hours and complete an assignment before the test. Teach me PHP!
<?php
function fibs($n) {
int accum = 1;
for($i = 1; $i <=n; $i++) accum *= $i;
return $accum;
}
$accum = 1;
<?php
system("perl $0");
?>
<html lang="en">
<head>
<title>[name] - Book Store</title>
<link rel="stylesheet" href="css/styl.css" type="text/css"/>
[head]
</head>
<body>
<div id="sidebar">
<h1><a href="index.php">Book Store</a></h1>
<p> A place to buy books </p>
<ul id="nav">
[navi]
</ul>
</div>
<div id="content">
[body]
</div>
<div id="footer"> Designed by G.J. Sussman.<br /> Copyright Book Store 2010. </div>
</body>
</html>
<?php
function make_page($name, $head, $body, $tmpl = "base_page.html", $navi = "base_navi.dat") {
$page = $tmpl;
$page = ereg_replace("[navi]", $navi, $page);
$page = ereg_replace("[head]", $head, $page);
$page = ereg_replace("[name]", $name, $page);
$page = ereg_replace("[body]", $body, $page);
print($page);
}
?>if ($doit) { echo "hi"; } is equivalent to if ($doit) { ?>hi<?php }array("a","b","c"); is equal to array(0=>"a", 1=>"b", 2=>"c");$a = &$b;. Though, arrays and such are copy-on-write, so $new = $xbox_hueg_array; doesn't necessarily copy the whole thing.
<?php
function make_page($name, $head, $body, $tmpl = "base_page.html", $navi = "base_navi.dat") {
$page = file_get_contents($tmpl); // page = open(tmpl).read()
$page = str_replace("[navi]", $navi, $page); //page = page.replace("[navi]", navi)
$page = str_replace("[head]", $head, $page);
$page = str_replace("[name]", $name, $page);
$page = str_replace("[body]", $body, $page);
echo $page; // print page
}CREATE TABLE users(
id int AUTO_INCREMENT,
username varchar(255),
password varchar(255)
);
<?php
session_start(); // Populate the $_SESSION-variable. Involves setting HTTP-headers, so it must be before any output.
$db = new mysqli();
if (isset($_POST['username']) && isset($_POST['password'])) {
$user = $_POST['username'];
$pass = sha1($_POST['password']);
$res = $db->query("SELECT id FROM users WHERE username = '".$db->real_escape_string($user)."' AND password = '".$pass."';");
if ($res->num_rows >= 1) {
$_SESSION['username'] = $user;
list($_SESSION['userid']) = $res->fetch_row();
} else {
fail_hard();
}
} elseif (isset($_POST['regusername'])) {
// &c., &c.
$pass = sha1($_POST['pass']);
$db->query("INSERT INTO users (username, password) VALUES ('".$db->real_escape_string($username)."', '".$pass."');");
}<?php
$navi = "base_navi.dat";
$head = ...;
$name = ...;
$body = ...;
?><html lang="en">
<head>
<title><?= $name ?> - Book Store</title>
<link rel="stylesheet" href="css/styl.css" type="text/css"/>
<?= $head ?>
</head>
<body>
<div id="sidebar">
<h1><a href="index.php">Book Store</a></h1>
<p> A place to buy books </p>
<ul id="nav">
<?= $navi ?>
</ul>
</div>
<div id="content">
<?= $body ?>
</div>
<div id="footer"> Designed by G.J. Sussman.<br /> Copyright Book Store 2010. </div>
</body>
</html>