Solved

advice on best solution to use

Posted on 2006-11-09
5
242 Views
Last Modified: 2006-11-18
some background information:

i have a PHP page, that when viewed, is customised to the user viewing it.  how i detect which user is viewing the page is by using an Java applet that i built to POST the username that windows stores after you login to our network.  in my PHP page i get that username like: $username = trim($_GET['username']);

the issue im facing and needing advice on is this:

when i get the username, i query MS SQL to get the users details stored in our system and i register a session.  (after i register my session, i can then customise the PHP page for that user)

now if the username is blank - i want to redirect to an error page as either the user has tired to edit the username in URL or the username didnt get posted correctly to the PHP page from the Java Applet (HTML page).

now i also want to check if the session is set or not (empty).  if its already set, skip the process of setting the session the second time around if user refreshes the PHP page.  if the session isnt set, the obviously set the session and show the PHP page customised to that user.  

But regardless of whether the session is set or not, my users are always redirected to error page...

my current code:

<?php
session_start();

$username = trim($_GET['username']);

if(!isset( $_SESSION['user_info']))
{
      $_SESSION['user_info'] = array();

      // DB connection string here
      // DB query string that uses $username and the result string of that query here

      while ($row = mssql_fetch_array($results))
      {
            $_SESSION['user_info']['persid'] = trim($row['PersID']);
            $_SESSION['user_info']['surname'] = trim($row['Surname']);
            $_SESSION['user_info']['firstname'] = trim($row['Firstname']);
      }
            
      // Close DB connection here
}

// Rest of HTML tags here that is used to customise the page for the user
?>
0
Comment
Question by:ellandrd
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
huji earned 500 total points
Comment Utility
>> now if the username is blank - i want to redirect to an error page as either the user has tired to edit the username in URL or the username didnt get posted correctly to the PHP page from the Java Applet (HTML page). <<
This is not a good practice. A user may change the username to something else (other than blank) and you may get into troubles. The only case that you should handle is not when the username is empty.

And about your code, I didn't see any part of it that should do the redirect (which you say is happening all the time) but eventually it should be changed to something like:

<?php
session_start();

if(isset($_GET['username'])){
    $username = trim($_GET['username']);
} else {
    //redirect to the login page.
    header("Location: login.php");
}

if(!isset( $_SESSION['user_info']))
{
     $_SESSION['user_info'] = array();

     // DB connection string here
     // DB query string that uses $username and the result string of that query here

     while ($row = mssql_fetch_array($results))
     {
          $_SESSION['user_info']['persid'] = trim($row['PersID']);
          $_SESSION['user_info']['surname'] = trim($row['Surname']);
          $_SESSION['user_info']['firstname'] = trim($row['Firstname']);
     }
         
     // Close DB connection here
}

// Rest of HTML tags here that is used to customise the page for the user
?>
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
hi huji

i removed the redirect code.  sorry i should have stated this was the only code i had working to an extent.

since opening the Question and you posted, this is now my latest code:

<?php
session_start();

if(empty($_SESSION['user_info']) && isset($_SESSION['user_info']))
{
        // session has been set already but empty so probably timed out
      header('Location:timeout.html');
      exit;
}
else if(empty($_SESSION['user_info']))
{
        // session not even set yet so set it now
      $_SESSION['user_info'] = array();

      $username = trim($_GET['username']);

      if (strtolower(trim($username)) == 'administrator')
      {
            $_SESSION['user_info']['firstname'] = 'Administrator';
            $_SESSION['user_info']['surname'] = '';
            $_SESSION['user_info']['username'] = 'administrator';
      }
      else
      {
            $Personnel_Connect = @mssql_connect("file04abzpi", "sa", "");
            $db = @mssql_select_db("Personnel",$Personnel_Connect);
            $query = "SELECT tblPersonnel.PersID,tblPersonnel.FirstName,tblPersonnel.Surname,tblPersonnel.NetworkLogin FROM tblPersonnel WHERE tblPersonnel.NetworkLogin = '".$username."'";
            $results = mssql_query($query) or die(mssql_error());

            while ($row = mssql_fetch_array($results))
            {
                  $FirstName = trim($row['FirstName']);
                  $Surname = trim($row['Surname']);

                  if($FirstName == 'William' && $Surname == 'Neilson')
                  {
                        $_SESSION['user_info']['firstname'] = 'Bill';
                  }
                  else
                  {
                        $_SESSION['user_info']['firstname'] = $FirstName;
                  }

                  $_SESSION['user_info']['persid'] = trim($row['PersID']);
                  $_SESSION['user_info']['surname'] = trim($row['Surname']);
                  $_SESSION['user_info']['username'] = trim($row['NetworkLogin']);
            }
            mssql_close($Personnel_Connect);
      }

        // refresh page so username isnt show in URL
      header('Location: index.php');
      exit;
}

// show page here
?>
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
ok, ive managed to it working.

i'll accept your first comment as you've pointed out that if username is blank, problems can occur... so thank you for this.

ellandrd
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
looks like ive just giving you your web development master cert too - well done!

ellandrd
0
 
LVL 14

Expert Comment

by:huji
Comment Utility
ellandrd,

Thanks for the points! I didn't even notice the new certificate, before you informed me!! Thanks again.

Huji
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

Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

762 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

10 Experts available now in Live!

Get 1:1 Help Now