-2

I am trying to create a checklogin.php file. I'm sure people reading this will understand, but to reiterate, I want to make sure the username and password are both in my database before going to the next screen.

Basically my issue is this, when I click Login, I am shown the PHP code of the checklogin.php file instead of directing to the next page or displaying an error at login.

My Login Page:

<html>
  <head>
    <title>Senior Project Login</title>
  </head>
  <body>
    <p>Please Login: </p>

    <form action="checklogin.php" method="POST"> 
       <p>Username: <input type="text" name="username" placeholder="Username" required="required" /> <br/>
       <p>Password: <input type="password" name="password" placeholder="Password" required="required" /> <br/><br>

       <input type="submit" value="Login"/>
       <input type="button" onclick="location.href='SeniorDB_Main.php';" value="Return to Register" />

    </form> 

  </body>
</html> 

My checklogin.php file:

<?php
    mysql_connect("localhost", "[db]", "[pw]") or die("mysql     connection is failure.");
    mysql_select_db("[db]") or die("Database does not exists.");

    if (isset($_POST['submit'])){
    $username=mysql_escape_string($_POST['username']);
    $password=mysql_escape_string($_POST['password']);
    if (!$_POST['username'] | !$_POST['password'])
     {
    echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('You did not complete all of the required fields')
            window.location.href='htmlogin.html'
            </SCRIPT>");
    exit();
         }


    $sql= mysql_query("SELECT * FROM `users` WHERE `username` = '$username'   AND `password` = '$password'");
    if(mysql_num_rows($sql) > 0)
    {
    echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('Login Succesfully!.')
            window.location.href='htmllogin.html'
            </SCRIPT>");
    exit();
    }
    else{
    echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('Wrong username password combination.Please re-enter.')
            window.location.href='htmllogin.html'
            </SCRIPT>");
    exit();
    }
   }
    else{
    }
?>

After doing a lot of research I have a few more questions. I also want to apologize, but my knowledge of this subject was vastly lower than I thought.

1) Do I have to install PHP? All this time I have just been creating files accessing databases using PHP, never installed anything.

2) How do I know how I am accessing this database? Currently, it is on a server provided by my professor and we just create the database and put the files in it. Do I have to install something into that as well?

3) I went to PHP code is not being executed, instead code shows on the page to see if I can work out a solution from there and everything was way over my head, I didn't understand what happened.

If anyone can explain (in newbie talk) what is happening that would be awesome.

Community
  • 1
  • 1
mudwomp
  • 105
  • 1
  • 1
  • 9
  • Small point: what if Javascript is disabled? – ScottMcGready Nov 25 '15 at 00:58
  • Are you running it from `localhost` or as file? – Mi-Creativity Nov 25 '15 at 01:00
  • Are you sure PHP is installed and working with Apache or a web server alternative? – Hurricane Development Nov 25 '15 at 01:02
  • I'm not sure myself. How do you do that? This is the first time I am running into this problem - kind of new to this – mudwomp Nov 25 '15 at 01:05
  • I am logging into a server to access the database, but it is a localhost. I'm sorry if I'm not too savvy on this – mudwomp Nov 25 '15 at 01:07
  • work through the suggestions in the dupe until you find the issue –  Nov 25 '15 at 01:08
  • I went to the dupe page and could not understand what is happening and how I should even tackle his solution. Do I have to install something? I have always used PHP as I have used HTML except with accessing databases. Since my files are accessing a server provided by my school, do I have to install something there? – mudwomp Nov 25 '15 at 08:35
  • you need to talk to your school to get the basics setup. –  Nov 25 '15 at 21:29

1 Answers1

-1

If you are seeing PHP code in the web browser, the web server isn't treating the file as PHP and parsing it, instead it's sending it out like it was a text or HTML file.

To test if PHP is installed and working correctly, create a page with the following code:

<?php
phpinfo();

If this outputs a table with info about your PHP install then it's working correctly. If you see that PHP code you'll need to install PHP in your web server.

For Apache on Linux, you'll need to add the PHP module to the web server's config:

LoadModule php5_module modules/libphp5.so

And then tell it PHP files are processed with the PHP module:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

The same basic thing would need to be done for other web servers, i.e add PHP module, associate PHP files with the PHP module.

The PHP install documentation should give you more information http://php.net/manual/en/install.php

Ryan Jenkin
  • 864
  • 8
  • 11
  • This is really confusing to me. I don't know PHP very well, I didn't even know I had to install something for it?? When I try to open the ` – mudwomp Nov 25 '15 at 08:24
  • Web servers work by sending files requested by the user (HTML, JPG, etc). PHP is different in that it's executed and the output of that execution is sent to the user. For this to happen PHP needs to be installed and the web server needs to know .php files are executed by the PHP module. The specifics on how to install depend on your operating system and web server. The PHP documentation has step by step instructions. – Ryan Jenkin Nov 25 '15 at 23:12