rgb192
asked on
how to load another user session. What code should I add
utilities.inc.php
index.php
session is null, $user is null
how can I have user have a value
<?php # utilities.inc.php - Script 9.3
// This page needs to do the setup and configuration required by every other page.
// Autoload classes from "classes" directory:
function class_loader($class) {
require('classes/' . $class . '.php');
}
spl_autoload_register('class_loader');
// Start the session:
session_start();
// Check for a user in the session:
$user = (isset($_SESSION['user'])) ? $_SESSION['user'] : null;
// Create the database connection as a PDO object:
try {
// Create the object:
$pdo = new PDO('mysql:dbname=cms;host=localhost', 'root', '');
} catch (PDOException $e) { // Report the error!
$pageTitle = 'Error!';
include('includes/header.inc.php');
include('views/error.html');
include('includes/footer.inc.php');
exit();
}
index.php
<?php # index.php - Script 9.7
// Need the utilities file:
require('includes/utilities.inc.php');
// Include the header:
$pageTitle = 'Welcome to the Site!';
include('includes/header.inc.php');
// Fetch the three most recent pages:
try {
$q = 'SELECT id, title, content, DATE_FORMAT(dateAdded, "%e %M %Y") AS dateAdded FROM pages ORDER BY dateAdded DESC LIMIT 3';
$r = $pdo->query($q);
// Check that rows were returned:
if ($r && $r->rowCount() > 0) {
// Set the fetch mode:
$r->setFetchMode(PDO::FETCH_CLASS, 'Page');
// Records will be fetched in the view:
include('views/index.html');
} else { // Problem!
throw new Exception('No content is available to be viewed at this time.');
}
} catch (Exception $e) { // Catch generic Exceptions.
include('views/error.html');
}
// Include the footer:
include('includes/footer.inc.php');
?>
session is null, $user is null
how can I have user have a value
What do you mean
I don't see where you are setting the session prior to checking it exists
ASKER
line 13,14 utilties.inc.php
// Check for a user in the session:
$user = (isset($_SESSION['user'])) ? $_SESSION['user'] : null;
what happens if there is/(is not) a user in the session
how can I simulate both
I think no user (user=null) is what happens currently
so how can I simulate user=something
// Check for a user in the session:
$user = (isset($_SESSION['user']))
what happens if there is/(is not) a user in the session
how can I simulate both
I think no user (user=null) is what happens currently
so how can I simulate user=something
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
solution works. Thanks