Page 1 of 1

[SOLVED] ID when logging in

Posted: Fri Feb 19, 2010 7:35 am
by aravona
I've made a log in code and I've now altered it to refresh you back to the index. So that you're shown as logged in.

Problem I'm having is with displaying the users ID. If I use $_SESSION['id'] it throws up a fuss saying its not recognised 'Undefined index: id'
If I use $_POST['txtid'], I dont get an error until after logging in and refreshing back to the index, saying the same thing 'Undefined index: txtid'. They're only notices but is there a way so that I don't get a notice :) even notices mean untidy coding.

Code: Select all

<?php
    session_start();
    include("validate.php");
    include("connection.php");
    
    if ($_SESSION['id'] != "") {
        echo "you are now logged in as ".$_SESSION['id'];
        echo "<form action='logout.php' method='post'><input name='Logout' type='Submit' value='Logout' /></form>";
        }
    else{
        // show login box 
 
?>
<html>
<head>
<title>Home Page</title>
<head>
<body>
   <form name="frmLogin" action="login.php" method="post">
        Username :
    <input name="txtid" type="text" />
        <br/><br>
        Password :
        <input name="txtpwd" type="password" />
        
        <input type="submit" value="Login" name="btnlogin" />
   </form>
</body>
</html>
 
<?php
    }
?>
Thanks,

Aravona

Re: ID when logging in

Posted: Fri Feb 19, 2010 7:46 am
by rahulzatakia
Hi,
can you tell me how you are storing the value in $_SESSION['id']..

Re: ID when logging in

Posted: Fri Feb 19, 2010 7:48 am
by aravona
erm in my validate.php

Code: Select all

 
    function validatelogin($id,$password){
       // Connect to the database
       include("connection.php");
       // Build an SQL statment that will return a record with a
           // matching id and password.
       $sql = "select username, password from userdetails where username='$id' and password= md5('$password');";
       $loginresult = mysql_query($sql,$conn);
       $userdetails = mysql_fetch_array($loginresult);
       // If the SQL query contains a record
       if ($userdetails['username']){
              $_SESSION['id'] = $id;
          return true;
           }
           else {
          $_SESSION['id'] ='';
          return false; 
           }
    }

Re: ID when logging in

Posted: Fri Feb 19, 2010 7:56 am
by rahulzatakia
From where you are calling the function validatelogin()..
You have given login.php in the action of login form...

Re: ID when logging in

Posted: Fri Feb 19, 2010 7:58 am
by aravona
Yeah, thats the following:

Code: Select all

 
 session_start();
    include("validate.php");
    include("connection.php");
 
    if ($_SESSION['id'] != '' or validatelogin($_POST['txtid'],$_POST['txtpwd']) == true){
    
    echo "<head><meta HTTP-EQUIV='REFRESH' content='0; url=index.php'></head>";
    }
    else 
    {
    echo "You have posted incorrect log in data you will automatically be redirected";
    echo "<head><meta HTTP-EQUIV='REFRESH' content='10; url=index.php'></head>";
    }
But you asked where i was setting the session ID so I thought you wanted the code for that function sorry :)

Re: ID when logging in

Posted: Fri Feb 19, 2010 8:01 am
by rahulzatakia
Just try...
start the session in your validate.php file...
i think your problem should be solved

Re: ID when logging in

Posted: Fri Feb 19, 2010 8:04 am
by aravona
That gets ignored:

A session had already been started - ignoring session_start() in D:\wamp\wamp\www\stuff\userlogin2\validate.php

Re: ID when logging in

Posted: Fri Feb 19, 2010 8:17 am
by rahulzatakia
i have tried your code in my local system and its working fine
after login i get the following message...

you are now logged in as rahul
and logout button.....

Re: ID when logging in

Posted: Fri Feb 19, 2010 8:20 am
by aravona
Well thats what the code does do. I'm not saying it doesnt work, it works but there is a NOTICE: before logging in and I don't want it there. Which means something must not be right.

If you don't have them turned on you wont see it. Obviously... lol - so can someone tell me how to get rid of the notice? (preferably without having to just turn off notices. I'd rather alter my code :) )

Re: ID when logging in

Posted: Fri Feb 19, 2010 8:42 am
by aravona
fixed it with:

Code: Select all

 
    if (!isset($_SESSION['id'])){
    $_SESSION['id'] = "undefined";
    } 
 

OR so I thought, this fixed the problem then completely messed the log out *sigh*