Wednesday, April 20, 2011


<?php
//cookie headers must be sent before any other headers
//to unset all the session variables at once, use $_Sesson = array();
session_start();
$_SESSION['sess_var']="Hello World!";

echo 'The content of $_SESSION[\'sess_var\'] is ' . $_SESSION['sess_var'] . '<br/>';

?>
<a href="page2.php">Next page</a>;


Here is another tutorial from the book PHP and MySQL web development


<?php
session_start();
//the scripts activites revolve around the valid_user session variable. the baseic idea is that if someone logs in successfully, you will register a session variable
//called $_SESSION['valid_user']
if (isset($_POST['userid']) && isset($_POST['password']))
{
// if the user has just tried to log in
$userid = $_POST['userid'];
$password = $_POST['password'];

$db_conn = new mysqli('localhost', 'webauth', 'webauth', 'auth');

if (mysqli_connect_errno()) {
echo "Connection to database failed: " . mysqli_connect_error();
exit;
}

$query = "select * from authorized_users " . "where name='$userid' " . " and password='$password'";

$result = $db_conn->query($query);
if ($result->num_rows)
{
// if they are in the database register the user id
$_SESSION['valid_user'] = $userid;
}

$db_conn->close();

}
?>
<html>
<body>
<h1>Home Page</h1>
<?php
if (isset($_SESSION['valid_user']))
{
echo 'You are logged in as: ' . $_SESSION['valid_user'] . '<
';
echo '<a href="logout.php">Log Out</a><br />';
}
else
{
if (isset($userid))
{
// if they have tried and failed to log in
echo 'Could not log you in. <br />';
}
else
{
// they have not tried to log in yet or have logged out
echo 'You are not logged in.<br />';
}

//provide form to log in
echo '<form method="post" action="authmain.php">';
echo '<table>';
echo '<tr><td>Userid:</td>';
echo '<td><input type="text" name="userid"></td></tr>';
echo '<tr><td>Password:</td>';
echo '<td><input type="password" name="password"></td></tr>';
echo '<tr><td colspan="2" align="center">';
echo '<input type="submit" value="Log In"></td></tr>';
echo '</table></form>';

}
?>
<br />
<a href="members_only.php">Members section</a>
</body>


Here is the continuation of that tutorial on sessions and log ins


<?php

session_start();

echo '<h1>Members Only';

// check session variable

if (isset($_SESSION['valid_user']))
{
echo '<p>You are logged in as ' . $_SESSION['valid_user'] . ' </p>';
echo '<p>Members only content goes here</p>';
}
else
{
echo '<p>You are not logged in.</p>';
echo '<p>Only logged in members may see this page.</p>';
}
echo '<a href="authmain.php">Back to main page</a>';
?>


Here is the final page from that tutorial / project



<?php

session_start();

//store to test if they *were* logged in
$old_user = $_SESSION['valid_user'];
unset($_SESSION['valid_user']);
session_destroy();
?>
<<html>
<body>
<h1>Log Out</h1>
<?php
if (!empty($old_user))
{
echo 'Logged out.<br />';
}
else
{
//if they weren't logged in but came to this page somehow
echo 'You were not logged in, and so have not been logged out<br/>';
}
?>

<a href="authmain.php">Back to the Main Page</a>
</body>


Here is another tutorial on arrays


<?php
$customer = array('first' => 'Bill', 'last' => 'Jones', 'age' => '24', 'street' => '123 Main St.', 'city' => 'Pacifica', 'state' => 'California');
extract($customer);
print "<p>$first $last is $age years old, and lives in $city, $state</p>";

extract($customer, EXTR_PREFIX_ALL, 'cust');
print "<p>$cust_first $cust_last is $cust_age years old, and lives in $cust_city, $cust_state.</p>";
?>

No comments: