Page 1 of 1

can any one help

Posted: Sun Jan 17, 2010 11:15 pm
by 5475427
can any one help me when i try out this code it shows if (!empty($_POST['username'])) in the login box how do i make it not show if (!empty($_POST['username']))

<?php
session_start();
// Check if he wants to login:
if (!empty($_POST['username']))
{
require_once("connect.php");

// Check if he has the right info.
$query = mysql_query("SELECT * FROM members
WHERE username = '$_POST[username]'
AND password = '$_POST[password]'")
or die ("Error - Couldn't login user.");

$row = mysql_fetch_array($query)
or die ("Error - Couldn't login user.");

if (!empty($row['username'])) // he got it.
{
$_SESSION['username'] = $row['username'];
echo "Welcome $_POST[username]! You've been successfully logged in.";
exit();
}
else // bad info.
{
echo "Error - Couldn't login user.<br /><br />
Please try again.";
exit();
}
}

?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h5>Login</h5></td>
</tr>
<tr>
<td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>
</body>
</html>

Re: can any one help

Posted: Sun Jan 17, 2010 11:48 pm
by pbs
Use isset(trim($_POST['username'])) instead of if (!empty($_POST['username']))

Re: can any one help

Posted: Mon Jan 18, 2010 12:14 pm
by McInfo
5475427 wrote:it shows if (!empty($_POST['username'])) in the login box
Do you mean it shows this in the username textbox?

Code: Select all

<? echo $_POST[username]; ?>
The quick answer is that your PHP configuration probably has the short_open_tag option disabled. Either enable it or, preferably, change "<?" to "<?php" in your script.

Also, read about SQL Injection and always use quotes around a string literal array index.
pbs wrote:Use isset(trim($_POST['username']))
isset() and trim() cannot be nested.

Edit: This post was recovered from search engine cache.

Re: can any one help

Posted: Mon Jan 18, 2010 5:22 pm
by 5475427
when i change it to isset(trim($_POST['username'])) it says Fatal error: Can't use function return value in write context in C:\wamp\www\new\register.php on line 4

here is the code agan

<?php
session_start();
// Check if he wants to login:
isset(trim($_POST['username']))
{
require_once("connect.php");

// Check if he has the right info.
$query = mysql_query("SELECT * FROM members
WHERE username = '$_POST[username]'
AND password = '$_POST[password]'")
or die ("Error - Couldn't login user.");

$row = mysql_fetch_array($query)
or die ("Error - Couldn't login user.");

if (!empty($row['username'])) // he got it.
{
$_SESSION['username'] = $row['username'];
echo "Welcome $_POST[username]! You've been successfully logged in.";
exit();
}
else // bad info.
{
echo "Error - Couldn't login user.<br /><br />
Please try again.";
exit();
}
}

?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h5>Login</h5></td>
</tr>
<tr>
<td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>
</body>
</html>

Re: can any one help

Posted: Mon Jan 18, 2010 5:41 pm
by SimpleManWeb
You forgot the "If" in your isset statement, try this code:

Code: Select all

 
<?php
    session_start();
    // Check if he wants to login:
    if (isset(trim($_POST['username']))) {
        require_once("connect.php");
 
        // Check if he has the right info.
        $query = mysql_query("SELECT * FROM members
        WHERE username = '$_POST[username]'
        AND password = '$_POST[password]'")
        or die ("Error - Couldn't login user.");
 
        $row = mysql_fetch_array($query)
    } else {
        die ("Error - Couldn't login user.");
    }
?>
 
 
Hope this helps

Re: can any one help

Posted: Mon Jan 18, 2010 5:52 pm
by 5475427
i still get the same error :(

Fatal error: Can't use function return value in write context in C:\wamp\www\new\login.php on line 4

when i change it to

<?php
session_start();
// Check if he wants to login:
if (isset(trim($_POST['username']))) {
require_once("connect.php");

// Check if he has the right info.
$query = mysql_query("SELECT * FROM members
WHERE username = '$_POST[username]'
AND password = '$_POST[password]'")
or die ("Error - Couldn't login user.");

$row = mysql_fetch_array($query)
} else {
die ("Error - Couldn't login user.");
}
?>


<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login.php" method="post">
<table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
<tr>
<td width="100%"><h5>Login</h5></td>
</tr>
<tr>
<td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td>
</tr>
<tr>
<td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td>
</tr>
<tr>
<td width="100%"><input type="submit" value="Login!"></td>
</tr>
</table>
</form>
</body>
</html>

Re: can any one help

Posted: Mon Jan 18, 2010 5:57 pm
by SimpleManWeb
Oh man, I should have looked at the code more closely. You can use functions like trim when you are doing things like isset.

Use this instead:

Code: Select all

 
<?php
    session_start();
    // Check if he wants to login:
    if (isset($_POST['username'])) {
        require_once("connect.php");
 
        // Check if he has the right info.
        $query = mysql_query("SELECT * FROM members
        WHERE username = '".trim($_POST[username])."'
        AND password = '".trim($_POST[password])."'")
        or die ("Error - Couldn't login user.");
 
        //$row = mysql_fetch_array($query)
    } else {
        die ("Error - Couldn't login user.");
    }
?>
 

Re: can any one help

Posted: Mon Jan 18, 2010 6:05 pm
by McInfo
5475427 wrote:when i change it to isset(trim($_POST['username'])) it says Fatal error
5475427 wrote:i still get the same error
Did you overlook my post? I said that nesting isset() and trim() is wrong. I also suggested that you should not use short open tags.

Edit: This post was recovered from search engine cache.

Re: can any one help

Posted: Mon Jan 18, 2010 6:06 pm
by 5475427
:( now all it says is Error - Couldn't login user. plzz help

Re: can any one help

Posted: Mon Jan 18, 2010 6:09 pm
by SimpleManWeb
That's good news, that means it worked. I put that code in there as a test and forgot to take it out. Last try. This one will work for sure:

Code: Select all

 
<?php
    session_start();
    // Check if he wants to login:
    if (isset($_POST['username'])) {
        require_once("connect.php");
 
        // Check if he has the right info.
        $query = mysql_query("SELECT * FROM members
        WHERE username = '".trim($_POST[username])."'
        AND password = '".trim($_POST[password])."'")
        or die ("Error - Couldn't login user.");
 
        //$row = mysql_fetch_array($query)
    }
?>
 
 

Re: can any one help

Posted: Mon Jan 18, 2010 6:19 pm
by 5475427
now it shows <? echo $_POST[username]; ?> in the login box and when u click login nothen happens like i put my user and pass and nothen hapend

Re: can any one help

Posted: Mon Jan 18, 2010 6:27 pm
by McInfo
Knock, knock.

Reread this topic.

Edit: This post was recovered from search engine cache.