can any one help

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
5475427
Forum Newbie
Posts: 10
Joined: Sun Jan 17, 2010 11:10 pm

can any one help

Post 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>
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: can any one help

Post by pbs »

Use isset(trim($_POST['username'])) instead of if (!empty($_POST['username']))
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: can any one help

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 4:21 pm, edited 1 time in total.
5475427
Forum Newbie
Posts: 10
Joined: Sun Jan 17, 2010 11:10 pm

Re: can any one help

Post 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>
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: can any one help

Post 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
5475427
Forum Newbie
Posts: 10
Joined: Sun Jan 17, 2010 11:10 pm

Re: can any one help

Post 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>
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: can any one help

Post 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.");
    }
?>
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: can any one help

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 4:22 pm, edited 1 time in total.
5475427
Forum Newbie
Posts: 10
Joined: Sun Jan 17, 2010 11:10 pm

Re: can any one help

Post by 5475427 »

:( now all it says is Error - Couldn't login user. plzz help
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: can any one help

Post 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)
    }
?>
 
 
5475427
Forum Newbie
Posts: 10
Joined: Sun Jan 17, 2010 11:10 pm

Re: can any one help

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: can any one help

Post by McInfo »

Knock, knock.

Reread this topic.

Edit: This post was recovered from search engine cache.
Post Reply