[SOLVED] ID when logging in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

[SOLVED] ID when logging in

Post 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
Last edited by aravona on Wed Feb 24, 2010 3:24 am, edited 3 times in total.
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: ID when logging in

Post by rahulzatakia »

Hi,
can you tell me how you are storing the value in $_SESSION['id']..
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: ID when logging in

Post 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; 
           }
    }
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: ID when logging in

Post by rahulzatakia »

From where you are calling the function validatelogin()..
You have given login.php in the action of login form...
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: ID when logging in

Post 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 :)
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: ID when logging in

Post by rahulzatakia »

Just try...
start the session in your validate.php file...
i think your problem should be solved
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: ID when logging in

Post by aravona »

That gets ignored:

A session had already been started - ignoring session_start() in D:\wamp\wamp\www\stuff\userlogin2\validate.php
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: ID when logging in

Post 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.....
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: ID when logging in

Post 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 :) )
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: ID when logging in

Post 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*
Post Reply