Simple PHP Login Script Problem

Discussion in 'Scripts' started by boingo, Mar 10, 2011.

  1. #1
    Hey,

    I am trying to use this simple php login script and it returns some error on row 6 ("if ($_SESSION["valid_user"])
    " row. It does however login properly, with right user and email ofcourse but returns the error

    "Notice: Undefined index: valid_user in c:\server\htdocs\login.php on line 6"

    Any master php dev up for fixing this in a sec :)

    Heres the login.php code, im using 3 more php files ( reg.php, welcome.php, logout.php)

    <?php
    session_start();
    // This part sets up the connection to the
    // database (so you don't need to reopen the connection
    // again on the same page).
    if ($_SESSION["valid_user"])
    {
    // User not logged in, redirect to login page
    Header("Location: welcome.php");
    }
    // is selected.
    if (isset($_POST['submit']))
    {
    $name = $_POST['name'];
    $email = $_POST['email'];

    $file = file_get_contents("data.txt");
    if(strstr($file, "$name||$email"))
    {

    $_SESSION["valid_user"] = $_POST["name"];
    $_SESSION["valid_time"] = time();

    // Redirect to member page
    Header("Location: welcome.php");
    }
    else
    {
    // Login not successful
    print '<script> alert ("Sorry! You have entered a Invalid Login Id or Password."); window.location="login.php"; </script>';
    }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Login Page Test Script.</title>
    </head>

    <body>
    <form method="post" action="login.php" >
    enter name:
    <input type="text" name="name" />
    <br/>
    <br/>
    enter email:
    <input type="text" name="email" />
    <br/>
    <br/>
    <input type="submit" value="login" name="submit"/>
    </form>
    </body>
    </html>
     
    boingo, Mar 10, 2011 IP
  2. bigtime

    bigtime Peon

    Messages:
    226
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace:

    if ($_SESSION["valid_user"])
    
    PHP:

    With this:
    
    if (isset($_SESSION['valid_user'])) 
    
    PHP:
    Hope that helps.

    Tim
     
    bigtime, Mar 14, 2011 IP
  3. arunsinghrawat

    arunsinghrawat Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hi,
    I am a PHP developer.I go through your message and i can resolve it if you are interested then let me know
     
    arunsinghrawat, Mar 14, 2011 IP
  4. boingo

    boingo Member

    Messages:
    87
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #4
    Thanks bigtime, that solved it and rep given.
     
    boingo, Mar 16, 2011 IP
  5. bigtime

    bigtime Peon

    Messages:
    226
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Glad it worked for you.

    Thanks,

    Tim
     
    bigtime, Mar 16, 2011 IP