Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Direct login (from index.php to member.php)

    Hi everyone,

    I have these php files:

    index.php
    login.php
    member.php
    logout.php


    Basically, I would like to be able to login directly to member.php, instead seeing a message "You are now logged in" on login.php and then needing to click to proceed. I don't find that suitable or necessary at all. But this is how tutorial instructed me to code.
    I have tried many things, but I'm not getting very far with this since I'm a newbie, so if anyone has an advice or a solution, I would be most grateful...
    Below are my files for reference:


    ---/index.php/----------------------------------------/index start/---

    <html>
    <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    </head>
    <body>
    <?php
    echo "<p class='title'>example.com / Demo / User Log In</p>
    <p class='lastedited'><a href='http://www.example.com'>Home</a> | <a href='http://www.example.com/demo'>Back to Demo</a> | <b>Last updated on March 29, 2011</b></p><br />";
    echo "<br /><br /><br />";
    echo "<div align='center' style='width: 300px; background-color: #FFF; border: 1px solid #0087D4; margin: 0px auto 0px auto; padding: 20px 0px 20px 0px; font-family: arial; font-size: 12px'>"."
    <form action='login.php' method='POST'><br /><br />
    Username: <input type='text' name='username' /><br />
    Password: <input type='password' name='password' /><br />
    <br />
    <input type='submit' value='Log in'>
    </form>";
    ?>
    </body>
    </html>

    ---/index.php/----------------------------------------/index end/---


    ---/login.php/----------------------------------------/login start/---

    <?php
    session_start();

    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username && $password)
    {
    $connect = mysql_connect("localhost","******","*****") or die ("Could not connect");
    mysql_select_db("session") or die ("Could not find db");

    $query = mysql_query("SELECT * FROM login WHERE username = '$username'");

    $numrows = mysql_num_rows($query);

    if ($numrows != 0)
    {

    while ($row = mysql_fetch_assoc($query))
    {
    $dbusername = $row['username'];
    $dbpassword = $row['password'];
    }
    // check to see if they match!
    if ($username == $dbusername && $password == $dbpassword)
    {
    echo "You are now logged in! <a href='member.php'>Click here to enter members area.</a>";

    $_SESSION['username']=$username;
    }
    else
    echo "<div style='font-family: Verdana; font-size: 12px;'>"."Incorrect password!"."</div>";
    }
    else
    die("<div style='font-family: Verdana; font-size: 12px;'>"."No such user in database!"."</div>");

    }
    else
    die ("<div style='font-family: Verdana; font-size: 12px;'>"."Please enter username and password!"."</div>");
    ?>
    ---/login.php/----------------------------------------/login end/---


    ---/member.php/---------------------------------/member start/---

    <?php
    session_start();

    if ($_SESSION['username'])

    echo "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."Welcome, "."<b>".$_SESSION['username']."</b>"."<b>!</b>";
    else
    die("<div style='font-family: Verdana; font-size: 12px;>"."You must be logged in!"."</div>");
    echo " | "."<a href='http://localhost/login/change.php'>Change password</a> | <a href='http://localhost/login/change.php'>Forgot password?</a> | <a href='logout.php'>Logout<a/>"."</div>";
    ?>

    ---/member.php/---------------------------------/member end/---


    ---/logout.php/---------------------------------/logout start/----

    <?php
    session_start();
    session_destroy();
    echo "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."You've been logged out. <a href='index.php'>Click here to return."."</div>";
    ?>

    ---/logout.php/---------------------------------/logout end/----

  • #2
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Would be glad if you can put the code inside [php] tags...

    Its the PHP icon with the page behind it on your editor at the top.


    Thanks.
    MY MSN: Sith717@Hotmail.com
    PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
    PM me anytime for HTML, PHP or web design help. I will be glad to help you out.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi there, thanks for replying. I hope this works for you. Not really sure what the end result should be.


    index.php
    PHP Code:
    <html>
    <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    </head>
    <body>
    <?php
    echo "<p class='title'>example.com / Demo / User Log In</p>
    <p class='lastedited'><a href='http://www.example.com'>Home</a> | <a href='http://www.example.com/demo'>Back to Demo</a> | <b>Last updated on March 29, 2011</b></p><br />"
    ;
    echo 
    "<br /><br /><br />";
    echo 
    "<div align='center' style='width: 300px; background-color: #FFF; border: 1px solid #0087D4; margin: 0px auto 0px auto; padding: 20px 0px 20px 0px; font-family: arial; font-size: 12px'>"."
    <form action='login.php' method='POST'><br /><br />
    Username: <input type='text' name='username' /><br />
    Password: <input type='password' name='password' /><br />
    <br />
    <input type='submit' value='Log in'>
    </form>"
    ;
    ?>
    </body>
    </html>
    login.php
    PHP Code:
    <?php
    session_start
    ();

    $username $_POST['username'];
    $password $_POST['password'];

    if (
    $username && $password)
    {
    $connect mysql_connect("localhost","******","*****") or die ("Could not connect");
    mysql_select_db("session") or die ("Could not find db");

    $query mysql_query("SELECT * FROM login WHERE username = '$username'");

    $numrows mysql_num_rows($query);

    if (
    $numrows != 0)
    {

    while (
    $row mysql_fetch_assoc($query))
    {
    $dbusername $row['username'];
    $dbpassword $row['password'];
    }
    // check to see if they match!
    if ($username == $dbusername && $password == $dbpassword)
    {
    echo 
    "You are now logged in! <a href='member.php'>Click here to enter members area.</a>";

    $_SESSION['username']=$username;
    }
    else
    echo 
    "<div style='font-family: Verdana; font-size: 12px;'>"."Incorrect password!"."</div>";
    }
    else
    die(
    "<div style='font-family: Verdana; font-size: 12px;'>"."No such user in database!"."</div>");

    }
    else
    die (
    "<div style='font-family: Verdana; font-size: 12px;'>"."Please enter username and password!"."</div>");
    ?>
    member.php
    PHP Code:
    <?php
    session_start
    ();

    if (
    $_SESSION['username'])

    echo 
    "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."Welcome, "."<b>".$_SESSION['username']."</b>"."<b>!</b>";
    else
    die(
    "<div style='font-family: Verdana; font-size: 12px;>"."You must be logged in!"."</div>");
    echo 
    " | "."<a href='http://localhost/login/change.php'>Change password</a> | <a href='http://localhost/login/change.php'>Forgot password?</a> | <a href='logout.php'>Logout<a/>"."</div>";
    ?>
    logout.php
    PHP Code:
    <?php
    session_start
    ();
    session_destroy();
    echo 
    "<div style='font-family: Verdana; font-size: 12px; border: 1px solid #43A5EC; background: #F0F0F0; padding: 10px;'>"."You've been logged out. <a href='index.php'>Click here to return."."</div>";
    ?>
    Last edited by tkm76; 03-31-2011 at 09:50 PM. Reason: code error

  • #4
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Edit your last post.

    The reason its good to use php tags is because its color coded and not alot of page scrolling is needed.

    You messed up on your last post.

    Missing some start php tags.
    MY MSN: Sith717@Hotmail.com
    PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
    PM me anytime for HTML, PHP or web design help. I will be glad to help you out.

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    instead of echoing that they can now proccedd to the members area just use this:

    PHP Code:
     header('Location: member.php'); 
    in:
    PHP Code:
    if ($username == $dbusername && $password == $dbpassword)
    {

    PUT REDIRECT HERE

    //echo "You are now logged in! <a href='member.php'>Click here to enter members area.</a>";
    $_SESSION['username']=$username;

    Have you ever tried a header redirect from the 'many things I tried'.

  • Users who have thanked bucket for this post:

    tkm76 (03-31-2011)

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It worked! Thank you!
    Yes I did use Header on several occasions, but I haven't coded php in two months. I guess I have a lot of catching up to do.

    Thanks again very much!


  •  

    Tags for this Thread

    Posting Permissions

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