In order to continue with labs in this course, it is now required that you setup and configure your development machine fully on your laptop. Those running non-windows machines cannot be supported by your professor (Windows only installations will be supported).
The following software must be installed and configured correctly.
We have already seen that we can share
files across our sites by include'ing them
(e.g. our headers and footers files).
We can also “require”
files:
Create an includes
sub-folder to your folder and create a file called functions.php
in that folder. You will also store your head.php
, header.php
, and footer.php
files in this folder as well.
Add the following code to the functions.php
file:
<?php
function db_connect(){
$connection = pg_connect("host=127.0.0.1 dbname=YOUR_DB_NAME user=YOUR_USERID password=YOUR_DB_PASSWORD" );
return $connection;
}
?>
Create a file called test.php
in the includes
folder.
<?php
include "header.php"; //nice to have file, formatting
require "./includes/functions.php";
/* a must have file, for db functionality (NOTE: this should in reality be placed in the header.php file. Why? */
?>
<!-- somewhere in the page do database stuff -->
<?php
$conn = db_connect(); // this works because the function exists in the "require"d file
pg_query($conn, “UPDATE table SET name = 'fred' WHERE id=1”);
// now can use the $conn connection resource
?>
<?php
include “footer.php”;
?>
int time();
time()
today ~45-50 years after 1970-1-1
returns approx. 1.5 billion
A week from now would be: $weekFromNow = time( ) + 60*60*24*7;
string date ( string $format [, int $timestamp ] )
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
$sql = "UPDATE users SET last_access = '". date("Y-m-d", time()) . "' WHERE id = '".$login."'";
Creates:
UPDATE users SET last_access = '2018-03-02' WHERE id = 'jdoe'
Another useful function of the date(); function is you can grab specific fields off of the optional timestamp
<?php echo date('Y'); ?>
NOTE: capital Y will display the current year as a 4-digit number
<?php //embed in a page with a two input box (named id and pass) form
require "./includes/functions.php";
$login = $_POST['id'];
$password = $_POST['pass'];
$conn = db_connect();
$sql = "SELECT first_name, last_name, email_address, last_access FROM
users WHERE id = '".$login."' AND password= '".$password."'";
//Note: the above is not a secure way to do this but ok for first learning
$results = pg_query($conn, $sql);
if(pg_num_rows($results)) {
//not zero means something was found
//user found, use pg_fetch_result to pull user specific info to display
} else {
//user not found, check for just login id
$sql = "SELECT * FROM users WHERE id = '".$login."'";
$results = pg_query($conn, $sql);
if(!pg_num_rows($results)) { //user not found, empty $login to unstick it
$login = ""; //when echo''ed in the form
}
}
?>