I'm trying to learn to program with PHP, and i got XAMPP. I watched a tutorial on youtube.
It should say, ''Please enter a username and a password''. But instead it just shows a blank page. My index=

<html>

      <form action='login.php' method='post'>
            Username: <input type='text' name=úsername'><br>
            Password: <input type='password'name='password'><br>
            <input type='submit' value='log in'>
      </form>
</html>

My Login.PHP

<html>
<?php

$username = $_POST('username');
$password = $_POST('password');

if ($username&&$password)
{

$connect = mysql_connect ("localhost","root","") or die("Couldn't connect");
mysql_select_db("phplogin") or die("Couldn't find db");

}
else
	die("Please enter a username and a password");

?>
</html>

I added the <html> and </html> in my login.php because when i opened my project in chrome, the login.php just gave the full code.

in your first code add form tag within the body tag...like
<html>
<body>
<form>
////your code goes here
</form>
</body>
</html>

and in login.php file, do some modification

mysql_select_db('phplogin',$connect);
or
mysql_connect ("localhost","root","") or die("Couldn't connect"); /*now you have connection to your db throughout the page*/
mysql_select_db("phplogin") or die("Couldn't find db");

both of them i guess will work..

remove the html from your login.php and add the body tags to your other file

INDEX.PHP

<html>
<body>
      <form action='login.php' method='post'>
            Username: <input type='text' name=úsername'><br>
            Password: <input type='password'name='password'><br>
            <input type='submit' value='log in'>
      </form>
</body>
</html>

LOGIN.PHP

<?php

$username = $_POST('username');
$password = $_POST('password');

if ($username&&$password)
{

mysql_connect ("localhost","root","") or die("Couldn't connect");
mysql_select_db("phplogin") or die("Couldn't find db");

}
else
	die("Please enter a username and a password");

?>

Still blank
http://imageshack.us/photo/my-images/121/loginphp.png/

The blank page you are getting is exactly what you are suppose to get. The reason is that you are only checking if the user has entered a username and password. Your script will only output "Please enter a username and a password" if the user does not provide a username and password. Follow this simple step to create your login.

index.html

<html>
<body>
      <form action='login.php' method='post'>
            Username: <input type='text' name=úsername'><br>
            Password: <input type='password'name='password'><br>
            <input type='submit' value='log in'>
      </form>
</body>
</html>

I'm going to assume that you have a third page where you direct users who provide valid username and password to. I will call this page members.php.

login.php

<?php
     //Clean user input
     $username = mysql_real_escape_string($_POST('username'));
     $password = mysql_real_escape_string($_POST('password'));

     if ($username&&$password)
     {

         $connect = mysql_connect ("localhost","root","") or die("Couldn't connect");
         mysql_select_db("phplogin") or die("Couldn't find db");
         $result = mysql_query("SELECT * FROM your_table_name WHERE username = $username AND password = $password") or die(mysql_error());
         if(mysql_num_rows($result) > 0) //Username and password match found
         {
              header("Location: members.php"); //Take user to members.php page
         }
         else //Username or password is invalid, notify user
         {
              echo "Invalid username or password. Please try again.";
         }
   }
    else
    {
	die("Please enter a username and a password");
    }

?>

members.php

<html>
<head>
    <title>MEMBERS ONLY PAGE</title>
</head>
<body>
  <?php
       echo "You are welcome";
  ?>
</body>
</html>

I know that it supposes to give that "please enter usr and pw" when i dont give in anything, still gets blank page, when I get home i'll check your code and put it in the existing file. Thanks for file!

This is want your script is doing

<?php

$username = $_POST('username');
$password = $_POST('password');

if ($username&&$password) //Check if user has supplied username and password
{

mysql_connect ("localhost","root","") or die("Couldn't connect"); //Connect to mysql server
mysql_select_db("phplogin") or die("Couldn't find db"); //Select database phplogin

//***************************************************************************
//Do something here if you want to see an output. For example, if you want to tell
//user "You are welcome", then do this

echo "You are welcome";

//***************************************************************************
}
else
	die("Please enter a username and a password");

?>

Thanks for the fast reply!
The green text can be deleted? Sorry, I'm really new to php.

yes green codes are just comments ....you can delete them....they are meant only for others to understand how the code works and are not executed....

WTF, I can not understand this.
Oke my Index.php file=

<html>
<body>
      <form action='login.php' method='post'>
            Username: <input type='text' name='username'><br>
            Password: <input type='password'name='password'><br>
            <input type='submit' value='log in'>
      </form>
</body>
</html>

Login.PHP file =

<?php
 
$username = $_POST('username');
$password = $_POST('password');
 
if ($username&&$password)
{

mysql_connect ("localhost","root","") or die("Couldn't connect");
mysql_select_db("phplogin") or die("Couldn't find db");
 


echo "You are welcome";


}
else
	die("Please enter a username and a password");

?>

If i put NOTHING in, blank page. If i put password and user BLANK page... I really don't know what i am doing wrong here..

Whoops, my database is correct, i deleted it, reinstalled it and checked again.

Your code should working after making the following corrections. Lines 3 and 4 should be

$username = $_POST['username'];
$password = $_POST['password'];

not

$username = $_POST('username');
$password = $_POST('password');

Still blank
Oke, my files are saved as index.php and login.php in the same folder.
If I open in google chrome, I have to put <html> in the login.php or it will only show code. In firefox i used index.php with this code:

<html>
<body>
      <form action='login.php' method='POST'>
            Username: <input type='text' name='username'><br>
            Password: <input type='password'name='password'><br>
            <input type='submit' value='log in'>
      </form>
</body>
</html>

My login.php is:

<?php

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{

mysql_connect ("localhost","root","") or die("Couldn't connect");
mysql_select_db("phplogin") or die("Couldn't find db");



echo "You are welcome";


}
else
	die("Please enter a username and a password");

?>

I am using the program: conTEXT.

I really don't know what is wrong, i'm running Windows 7.
Really stressing right now, I so wanna learn this programming language...

Quick question- are you accessing these pages with 'localhost' in your URL? The reason I ask is because you said you were getting the full code in Chrome, which shouldn't happen if you're accessing the pages through your Xampp server.

I mean this code that i see in chrome:

<?php
 
$username = $_POST['username'];
$password = $_POST['password'];
 
if ($username&&$password)
{
 
mysql_connect ("localhost","root","") or die("Couldn't connect");
mysql_select_db("phplogin") or die("Couldn't find db");
 
 
 
echo "You are welcome";
 
 
}
else
	die("Please enter a username and a password");
 
?>

file:///C:/Users/Dennis/Desktop/Code/index.php this is what is in the URL? How can i run the index.php in my localhost?

I think I have the problem. I'm not running it ok..
My index.php is in the code: http://localhost/bureaublad/code/index.php
bureaublad= a dutch word.

Now it says, object not found. Any solutions to this?

Ok, as I thought. You're accessing your stuff through the browser as a local file and not through the server.

Remeber that a browser can only read HTML, JavaScript, and other client-side code. PHP is server-side, so you need to access it through your server (on 'localhost').

The solution to your issue depends on your Xampp installation, so if you installed Xampp to the location 'C:\Xampp' (which is default), you should go to 'C:\Xampp\htdocs' and in there, make a folder (I'll call it 'bureaublad' in this example) and paste your 'index.php' and 'login.php' files into that folder.

Then (as long as Xampp and Apache are running), you should be able to access you code with a URL like 'http://localhost/bureaublad/index.php'.


Currently, since you're accessing the files directly in your browser instead of letting them be processed through the server, the server isn't being asked to do anything and you can't use PHP as a result (because PHP needs to be processed through the server- it can't be done by the browser alone).

Wow, thank you for your kind help. This helped me alot and finally it worked. Pfoehoe so happy lol :P

Thanks for the explanation, i've seen it in a course now you're telling me this. PHP is server-side and not client side :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.