www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] read file, create session, not working!

  1. #1
    Join Date
    Aug 2004
    Location
    San Antonio, TX
    Posts
    564

    resolved [RESOLVED] read file, create session, not working!

    PHP Code:
    $pass_entered md5($_POST['password']);
    $file "filename.php";
    $text file($file) or die("unable to read file");
    $fh fopen($file'r') or die("unable to open file");
    foreach(
    $text as $line)
    {
        list(
    $name$pass) = explode(','$line);
        if (
    $pass_entered == $pass)
        {
            
    $_SESSION['section'] = $name;
        }
    }
    fclose($fh); 
    This code is not setting the session. I know for sure the passwords are correct and i am entering them in correctly. why is the script not finding it in the file?

    if I change the foreach loop to print out the results of the file, they show up - so why is it not setting the section when it matches teh passwords?

    i guess the problem is the pass from list() isn't matching the entered pass, but i know that it's correct??

    the file looks like:
    PHP Code:
    <?php
    name
    ,pass
    name
    ,pass
    ?>
    ??

    GO GATORS!!!

    Answers to all your questions can be found at: PHP Manual

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Every page that is to set or get session data needs to start with a session_start() before you reference the $_SESSION array (and before any sort of output is sent to the browser).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    San Antonio, TX
    Posts
    564
    Correct! I have session_start() at teh top of the page, I even have an error check after that script to set an error session if the section isn't set and it works.

    Do you see any reason why the code isn't working? Do it seem to be right?

    Answers to all your questions can be found at: PHP Manual

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Try this to get rid of any trailing newlines:
    PHP Code:
    if ($pass_entered == rtrim($pass)) 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    San Antonio, TX
    Posts
    564
    that did it - nice look on the site, BTW

    Answers to all your questions can be found at: PHP Manual

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by rch10007
    PHP Code:
    $pass_entered md5($_POST['password']);
    $file "filename.php";
    $text file($file) or die("unable to read file");
    $fh fopen($file'r') or die("unable to open file");
    foreach(
    $text as $line)
    {
        list(
    $name$pass) = explode(','$line);
        if (
    $pass_entered == $pass)
        {
            
    $_SESSION['section'] = $name;
        }
    }
    fclose($fh); 
    There is something wrong there. You are using the password to check for the username. This assumes no two users will ever pick the same password. By the way why are you using the file system instead of a DB for this?

    Also you have an unnecessary fopen() and fclose() in there.

  7. #7
    Join Date
    Aug 2004
    Location
    San Antonio, TX
    Posts
    564
    i see now that i didn't need to open the file if i read it into an array, duh!

    i am using a file system because that is the requirement by my teacher. we have to make a small independent cms(sort of).

    the purpose of the password protection is so that all users can access only one directory. i have 3 directories (Section_1, 2, and 3). The way I can tell which directory to send the user is by the password they enter. there is no username. what would be the username is actually the name of the directory that the password is associated with. so when the pass matches, i read the name and that is the directory i send them to.

    i thought this was the easiest way to direct a user to a directory depending on the password given. do you have a suggestion of an easier way - the less code the better if you ask me! i love learning php - it's awesome!

    anyways - thanks to you both for helping, that pesky "\n" was a real pain in my arse.

    Answers to all your questions can be found at: PHP Manual

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Hmm. Well if you want to cut down on code the following should do that. I haven't tested it but I am certain it should work.
    PHP Code:
    if(preg_match('/(.+)[,]'.md5($_POST['password']).'/'file_get_contents('filename.php'), $matches))
    {
        
    $_SESSION['section'] = $matches[1];


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles