Transcript Document

PHP, Databases, and
Cookies
Dave Pease
IDS496
12/2/2003
www.dpease.com/php
PHP Basics

Scripting language





Non-compiled
Similar in operation to Perl
Similar in syntax to C
Designed for the Web
Releases exist for all major operating systems and
HTTP servers

MS Windows/MS IIS, Linux/Apache, etc.
PHP Syntax




Server executes anything
between <?php ?> as PHP
code
# or // for comments
Lines end in semi-colons;
Generally all files with .php
extensions are executed

Can be changed in HTTP server
configurations
PHP and HTML

PHP can be embedded in HTML

Anything outside <?php ?> is ignored by PHP but
rendered by HTTP server as normal
Passing Arguments to PHP

PHP can read command line arguments
No argument
arg=foo
PHP and Forms

PHP can read data from HTML forms
•Check functions can be
easily implemented
•Select boxes in forms can
be populated in loops in
PHP
•HTML page content always
displays
•Utilize PHP’s helpful
environmental variables
Databases and PHP

For content-heavy (article based) websites, PHP with
a database back-end is an excellent solution


Very few scripts and pages to maintain
Easy to change features/colors/layout across entire site
quickly
Site visitor
Single PHP page
Database
(many articles)
Database types

MySQL



PostgreSQL


Popular, many users, lots of documentation, excellent
integration with PHP
MyPHPAdmin is a popular, free tool that allows you to
administer your MySQL database via the web
Newer, more features, smaller user base
Both databases:


Are free
Support SQL instruction set (IDS 480)
Cookies

Information a website can
write to a client


Client must have cookies
enabled in their browser
Many legitimate uses


A site can only read a cookie
that it put on the client’s
computer, so allowing
cookies won’t throw your
entire hard drive open to
unscrupulous websites to
invade
PHP has built-in support for
cookies
Cookie Example: sessiontoken

When a user visits a site, PHP script checks for
cookie




If it exists, get unique sessiontoken from cookie to check
login status in database
If it doesn’t exist, create cookie with new sessiontoken
and create new row in database
Script “included” in every PHP page in site
Regular visitors/site members stay “logged in” and
don’t have to re-enter their passwords
Resources

php.net



Describe what you are trying to do
and include the string “PHP” and
you will nearly always find the
answer documented somewhere on
the internet
postgresql.com

PostgreSQL database
documentation
mysql.com


Good place to get answers to tough
PHP/database questions
google.com


Every PHP function documented
experts-exchange.com


phpmyadmin.net


Excellent technical books
editplus.com


Administrate a MySQL database
via the web
oreilly.com


MySQL database documentation
Notepad replacement/script editor;
shareware
http://www.chiark.greenend.org.uk/
~sgtatham/putty/

PuTTY free terminal utility