Solved

how to load another user session. What code should I add

Posted on 2014-03-23
4
355 Views
Last Modified: 2014-03-23
utilities.inc.php
<?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();
    
}

Open in new window


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');
?>

Open in new window




cookie is loaded quickly

session is null, $user is null

how can I have user have a value
0
Comment
Question by:rgb192
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39948748
What do you mean
how to load another user session
I don't see where you are setting the session prior to checking it exists
0
 

Author Comment

by:rgb192
ID: 39948760
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
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39948763
Just set it to something before that line

$_SESSION['user']="user1";
0
 

Author Closing Comment

by:rgb192
ID: 39948817
solution works. Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 48
windows 10 bash shell 4 38
Why does this login script not work on a live server? 19 42
php connect() failed error 25 17
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now