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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy help member area problem (login-session

    at first.. sory if my english bad

    um.. i have problem with my login php script with session..
    i have 3 file..

    1.login.php (this where user login)

    <form action="checklogin.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('nama','','R','password','','R');return document.MM_returnValue">
    <table width="200" border="0">
    <tr>
    <td>Nama</td>
    <td><input type="text" name="nama" id="nama" /></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password" id="password" /></td>
    </tr>
    </table>
    <p>
    <label>
    <input type="submit" name="button" id="button" value="Submit" />
    </label></p>
    </form>

    (with html tag ofcourse)

    then the form action is direct to checklogin.php

    this is script inside checklogin.php

    <?php
    // Connect to server and select databse
    mysql_connect("localhost","root","123");
    mysql_select_db("soundy")or die("cannot select DB");
    // Define $myusername and $mypassword
    $myusername=$_POST['nama'];
    $mypassword=$_POST['password'];
    $sql="SELECT * FROM user WHERE nama='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_start();
    $_SESSION['loggedin'] = true;
    header('Location: loginsuccess.php');
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>


    until here.. there is no problem.. because if i put username and password that is not in database... then its show Wrong username or password)

    then this is the problem.. (member area)

    this is script inside loginsuccess.php

    <?php
    session_start();
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    header('Location: login.php');
    }
    else
    {
    echo 'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>


    the question is why i always directed back to login.php? although i put the right username and password

    help me plss >.<

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    session_start() at the top of the page?
    comment this line out
    header('Location: loginsuccess.php');
    and add 1 line below in print_r($_SESSION) to see if it co ntains a value for loggedin => true

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    I commented out the sql code and set $count to 1 to fulfill your if statement and was redirected to the success page. This leads me to believe that there is something wrong with your sql statements.

    Change your sql statement to this:
    PHP Code:
    $myusername=$_POST['nama'];
    $mypassword=$_POST['password'];
    $sql "SELECT COUNT(*) FROM user WHERE nama='$myusername' and password='$mypassword'";
    $result mysql_query($sql);
    $count mysql_result($result,0);
    if (
    $count==1) {
        
    session_start();
    $_SESSION['loggedin'] = true;
    header('Location: loginsuccess.php');
    exit;
    }
    else {
    echo 
    "Wrong Username or Password";

    to see if this works
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    session_start() at the top of the page?
    comment this line out
    header('Location: loginsuccess.php');
    and add 1 line below in print_r($_SESSION) to see if it co ntains a value for loggedin => true
    ok rafiki... i already made change with this script as your request.. so this is it

    <?php
    // Connect to server and select databse
    mysql_connect("localhost","root","123");
    mysql_select_db("soundy")or die("cannot select DB");
    // Define $myusername and $mypassword
    $myusername=$_POST['nama'];
    $mypassword=$_POST['password'];
    $sql="SELECT * FROM user WHERE nama='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_start();
    $_SESSION['loggedin'] = true;
    print_r($_SESSION);
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>


    and the result shown is

    Array ( [loggedin] => 1 )

    but why i cannot enter the member area after i loggedin?

    this is script in the member area

    <?php
    session_start();
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    header('Location: login.php');
    }
    else
    {
    echo 'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by daemonkin View Post
    I commented out the sql code and set $count to 1 to fulfill your if statement and was redirected to the success page. This leads me to believe that there is something wrong with your sql statements.

    Change your sql statement to this:
    PHP Code:
    $myusername=$_POST['nama'];
    $mypassword=$_POST['password'];
    $sql "SELECT COUNT(*) FROM user WHERE nama='$myusername' and password='$mypassword'";
    $result mysql_query($sql);
    $count mysql_result($result,0);
    if (
    $count==1) {
        
    session_start();
    $_SESSION['loggedin'] = true;
    header('Location: loginsuccess.php');
    exit;
    }
    else {
    echo 
    "Wrong Username or Password";

    to see if this works
    i think my code as shown above no need to change..becaus if i put the wrong pass and wrong user.. the code will run the echo "wrongusernameandpassword" else directed to loginsucces.php


  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    session_start
    ();
    print_r($_SESSION);
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    header('Location: login.php');
    }
    else
    {
    echo 
    'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    tsifutokai: Did the code work?

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by daemonkin View Post
    tsifutokai: Did the code work?

    D.
    yes.

  • #9
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    PHP Code:
    <?php
    session_start
    ();
    print_r($_SESSION);
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    header('Location: login.php');
    }
    else
    {
    echo 
    'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>
    not working.. it directed back to login.php

  • #10
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    session_start
    ();
    print_r($_SESSION);
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    //header('Location: login.php');
    }
    else
    {
    echo 
    'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>
    strange you should have got an error headers already sent message.
    try commenting out the header() and make sure you have the print_r($_SESSION)
    under session start.

  • #11
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Post the code that you now have on checklogin.php
    please.

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #12
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rafiki View Post
    PHP Code:
    <?php
    session_start
    ();
    print_r($_SESSION);
    // is the one accessing this page logged in or not?
    if (!isset($_SESSION['loggedin'])) {
    //header('Location: login.php');
    }
    else
    {
    echo 
    'nice';
    }
    ?>
    <html>
    <body>

    WELCOME
    </body>
    </html>
    strange you should have got an error headers already sent message.
    try commenting out the header() and make sure you have the print_r($_SESSION)
    under session start.

    wow how about if there is no logged in user? that mean they can view content which specialy for login user.... (without redirection only for not logged in user)

  • #13
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by daemonkin View Post
    Post the code that you now have on checklogin.php
    please.

    D.
    i have post them before... ok.

    this is original code from me

    PHP Code:
    <?php
    // Connect to server and select databse
    mysql_connect("localhost","root","123");
    mysql_select_db("soundy")or die("cannot select DB");
    // Define $myusername and $mypassword 
    $myusername=$_POST['nama']; 
    $mypassword=$_POST['password']; 
    $sql="SELECT * FROM user WHERE nama='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_start();
    $_SESSION['loggedin'] = true;
    header('Location: loginsuccess.php');
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?>

  • #14
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ithink the problem is between checklogin.php and logginsucess.php..

    because : 1. i have logged in.. but in the logginsucces.php i directed back to login.php because this script "if (!isset($_SESSION['loggedin']))"

    need help >.<

  • #15
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    // Connect to server and select databse
    mysql_connect("localhost","root","123");
    mysql_select_db("soundy")or die("cannot select DB");
    // Define $myusername and $mypassword 
    $myusername=$_POST['nama']; 
    $mypassword=$_POST['password']; 
    $sql="SELECT * FROM user WHERE nama='$myusername'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    while ($count=mysql_fetch_array($result)){
    if (
    $count['password'] == $password){

    session_start();
    $_SESSION['loggedin'] = true;
    header('Location: loginsuccess.php');
    }else{

    echo 
    "Wrong Username or Password";
    }
    ?>
    use that in login.php
    loginsuccess.php should have
    PHP Code:
    if ($_SESSION['loggedin'] != true){
    include(
    login.php);
    //login
    }else{
    echo 
    "you are logged in please continue!";

    Try that.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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