SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with sessions

    hello, I'm a newbie. I'm wondering if anyone could help me on something. I have a login in form that takes me to this php file. I want the sessions for both staff and student to last through all of the links that are available to them but I'm not sure how, cos most sites have conflicting information or is not supported on php 5. Here is what I have so far (now don't laugh cos I'm not very good at this stuff, but i have to do it as part of my course)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head><title>Assignment Administration</title></head>
    <body>
    <?php

    require_once("http://www.tipsymich.tvu.farstate.net/assignment/includes/includeFiles.inc");

    ?>
    <?php


    if (isset($_POST['login']))
    {
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $Query = @mysql_query("SELECT username, password, usertype FROM login WHERE username = '$username' AND password = '$password'");
    $row = mysql_fetch_array($Query);
    $usertype = $row ['usertype'];

    $num_rows = mysql_num_rows($Query);

    if($num_rows < 1){
    print("Sorry, you're login details were not recognised, Please use the back button on you're browser to try again.");
    }
    if ($Query)
    {

    $_SESSION['authorised'] = TRUE;

    }



    }

    if (isset($_REQUEST['logout']))
    {
    unset($_SESSION['authorised']);
    }

    if (isset($_SESSION['authorised']))
    {

    if ($usertype == 'Staff')
    {

    ?>
    <h1>Staff Administration</h1>
    <p><a href="postass.php">Post an assignment</a></p>
    <p><a href="stuass.php">View Student's Assignments</a></p>
    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?
    logout=1">Logout</a></p>



    <?php
    } else if ($usertype == 'Student') {

    ?>

    <h1>Student Administration</h1>

    <p><a href="submitass.php">Submit an Assignment</a></p>
    <p><a href="postass/">View Posted assignments</a></p>
    <p><a href="viewfedbak.php">View feedback</a></p>
    <p><a href="viewgrade.php">View Grade</a></p>
    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?logout=1">Logout</a></p>


    <?php
    }
    } else echo'<h1> You are not authorised to view this site!</h1>'
    ?>
    </body>
    </html>

  2. #2
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    please hlp

    I'm stuck on this please help!

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before a session will register (or be resumed on a new page), you have to start the session engine with session_start(). Also, this must be called prior to any other output since it involves headers, so place it at the top of your script.

    Hope that helps.

    *Edit: something like this:

    Code:
    <?php session_start(); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head><title>Assignment Administration</title></head>
    <body>
    <?php
    
    require_once("http://www.tipsymich.tvu.farstate.net/assignment/includes/includeFiles.inc");
    
    ?>
    <?php
    
    
    if (isset($_POST['login']))
    {
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $Query = @mysql_query("SELECT username, password, usertype FROM login WHERE username = '$username' AND password = '$password'");
    $row = mysql_fetch_array($Query);
    $usertype = $row ['usertype'];
    
    $num_rows = mysql_num_rows($Query);
    
    if($num_rows < 1){
    print("Sorry, you're login details were not recognised, Please use the back button on you're browser to try again.");
    }
    if ($Query)
    {
    
    $_SESSION['authorised'] = TRUE;
    
    }
    
    
    
    }
    
    if (isset($_REQUEST['logout']))
    {
    unset($_SESSION['authorised']);
    }
    
    if (isset($_SESSION['authorised']))
    {
    
    if ($usertype == 'Staff')
    {
    
    ?>
    <h1>Staff Administration</h1>
    <p><a href="postass.php">Post an assignment</a></p>
    <p><a href="stuass.php">View Student's Assignments</a></p>
    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?
    logout=1">Logout</a></p>
    
    
    
    <?php
    } else if ($usertype == 'Student') {
    
    ?>
    
    <h1>Student Administration</h1>
    
    <p><a href="submitass.php">Submit an Assignment</a></p>
    <p><a href="postass/">View Posted assignments</a></p>
    <p><a href="viewfedbak.php">View feedback</a></p>
    <p><a href="viewgrade.php">View Grade</a></p>
    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?logout=1">Logout</a></p>
    
    
    <?php
    }
    } else echo'<h1> You are not authorised to view this site!</h1>'
    ?>
    </body>
    </html>

  4. #4
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tip: if you are planning to use sessions on all your pages, set session_auto_start to ON/1 in your php.ini file as this saves you calling session_start on each page...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •