We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help With A Simple Login PHP

Here Is My Index.php

<form action="login.php" method="post">
Username: <input type="text" name="usname"/></br>
Password: <input type="text" name="pwname"/></br>
<input type="submit" />
</form>

And Here is my login.php

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("logindb", $con);
    $user = $_POST['user'];
    $pw = $_POST['password'];

    $sql = "select username,password,email from users where username='" . $user. "' and password='" . $pw . "' limit 1";
    $result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());

    if (mysql_num_rows($result))
    {
    setcookie("user", $user , time()+3600);
    echo ("You Have Successfully Logged In</br>");
    }

    else
    {
        echo ("You Have Failed To Logged In</br>");
    }
?>
<a href="/index.php">CLick here To go back Home</a> 

And It Always Say Failed

4
Contributors
18
Replies
6 Hours
Discussion Span
11 Months Ago
Last Updated
19
Views
tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi,
Change in your form with this:

Username: <input type="text" name="user"/></br>
Password: <input type="text" name="password"/></br>

"name" value and $_POST['value'] must be the same value.

MarPlo
Junior Poster in Training
55 posts since Apr 2012
Reputation Points: 1
Solved Threads: 15
Skill Endorsements: 0

thanks but now when i try to save cokkie it says
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\login.php:5) in C:\xampp\htdocs\login.php on line 33

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I Solved It Thanks

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try

    $user = $_POST['usname'];
    $pw = $_POST['psname'];

     if (mysql_num_rows($result) >=1)
     {
         setcookie("user", $user , time()+3600);
         echo ("You Have Successfully Logged In</br>");
     }

If this doesn't work, then unset the cookie and do it again. And,let us know how you go!

rotten69
Master Poster
747 posts since May 2011
Reputation Points: 36
Solved Threads: 47
Skill Endorsements: 13

do you know how to hide some html items with php??

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

How do you exactly mean? If you're talking about what I am thinking, then you use client-side languages(javascript, jquery)

rotten69
Master Poster
747 posts since May 2011
Reputation Points: 36
Solved Threads: 47
Skill Endorsements: 13

Change the password type attribute:

<form action="login.php" method="post">
    <label for = "user">Username: </label><input type="text" name="user" id="user" />
    <label for = "password">Password: </label><input type="password" name="password" id="password" /></br>
<input type="submit" name="sublog" value="Login" />
</form>

Your php:

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("logindb", $con);
    $user = mysql_real_escape_string($_POST['user']);
    $pw = mysql_real_escape_string($_POST['password']);

    $sql = "SELECT username, password, email FROM users WHERE username='$user' LIMIT 1";
    $result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
    if (mysql_num_rows($result)){
         $data = mysql_fetch_assoc($result);
         //IDEALLY you'd hash your pw with a salt - this is not very secure
         if($pw == $data['password']){
             setcookie("user", $user , time()+3600);
             echo "You Have Successfully Logged In<br />";
         }
    }else{
        echo "You Have Failed To Logged In<br />";
    }
?>
<a href="/index.php">CLick here To go back Home</a> 
diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

I don't think he is looking that much security at this stage. But, it is a good intro to SQL injection!

rotten69
Master Poster
747 posts since May 2011
Reputation Points: 36
Solved Threads: 47
Skill Endorsements: 13

Thanks all but
there is a problem now i want to add html code to php if-else statements (to show or hide something)
i mean like that but when i use echo like

<?php
echo "<form action="login.php" method="post">
    <label for = "user">Username: </label><input type="text" name="user" id="user" />
    <label for = "password">Password: </label><input type="password" name="password" id="password" /></br>
<input type="submit" name="sublog" value="Login" />
</form>";
include 'menu.php';
?>

it says this error
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\index.php on line 2

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

up solved but have a new error :S
Here is Code

<?php
$cookie = $_COOKIE["userforcong"];
if (isset($_COOKIE["user"]))
            {
            echo '</br>Congratulations You have registered And logged in with your account ( <?php echo "$cookie"; ?> ).</br>';
                echo '<form action="" method="post">
                        <input type="submit" name="logout" value="Logout" />
                        </form>';


            }
            else
            {
                echo 'Please Login And Come back again';
            }




    function logout()
    {

        if (isset($_COOKIE["user"]))
            {
                setcookie("user", "", time()-3600);
                echo '</br>Logout Successfully</br>';
            }

    }

if (isset($_POST['logout']))

{
     logout();
}

?>

<html>
</html>

But i Get this error
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cong.php:5) in C:\xampp\htdocs\cong.php on line 25

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try:

echo "<br />Congratulations You have registered And logged in with your account ($cookie)<br />";

Also, you should check for the existence of a superglobal ($_COOKIE) before ascribing it to another var:

$cookie = $_COOKIE["userforcong"];

This will end in tears if $_COOKIE["userforcong"] does not exist.

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

i made COOKIE userforcong
im sure it exist

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

and still same error

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Ah! I was looking at line 5 in the error message - you're problem is at line 25:

function logout()
    {
        if (isset($_COOKIE["user"]))
            {
                setcookie("user", "", time()-3600);
                echo '</br>Logout Successfully</br>';
            }
    }

You can't set a cookie once you've echoed stuff to the screen or have got any html/whitespace in the page (http://php.net/manual/en/function.setcookie.php). I'm assuming that your page is included inside another file? This file seems to have output before the include statement. I may be mistaken.

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61
when i tried to type in 
`           echo ('hey.'.$cookie.'');`
            it gave me a good response idk why it doesnt wanna work
            P.s It Works With the successfull statement (after edit)
tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

its not that problem i have echoed another cookie it didnt touch user cookie

tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

i made it i just made the button to redirect to a page that only have this code and it worked

<?php
    function logout()
    {

        if (isset($_COOKIE["user"]))
            {
                setcookie("user", "", time()-3600);
                echo '</br>Logout Successfully</br>';
            }

    }
    logout();
?>
tarekwiz
Newbie Poster
11 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Great. Solved?

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1183 seconds using 2.73MB