2

I cannot seem to login and redirect with my code! I have tried for a while now but I cannot see to find the mistake as I am sure I have used this code before.

Please could you help me with telling me where I went wrong?

index.php:

<?php
    //includes login script
    include( 'login.php' );
?> 

<!doctype html>
<html>
    <head>  
        <title>Home Page</title>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/template.css" rel="stylesheet" type="text/css">
<script src="respond.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>
    <body>
    <div id="login">
      <h2>- Login -</h2>
                <hr/>
                <form action="" method="post">
                    <label>UserName  :</label>
                  <input type="text" name="username" id="name" placeholder="username"/><br /><br />
                    <label>Password  :</label>
                  <input type="password" name="password" id="password" placeholder="**********"/><br><br>
                    <br><br>
                  <input type="submit" value=" Login " name="submit"/><br />
                    <span><?php echo $error; ?></span>
                </form>
            </div>
    </body>
</html>

Login.php

<?php
    //starting session
    session_start();

    //variable to store error message
    $error=''; 

    if ( isset( $_POST[ 'submit' ] ) ) 
    {
        if ( empty( $_POST[ 'username' ] ) || empty( $_POST[ 'password' ] )  ) 
        {
            $error = "Information is invalid"; 
        } else {   
            // Define $username and $password 
            $username = $_POST[ 'username' ]; 
            $password = $_POST[ 'password' ]; 

            // To protect MySQL injection for Security purpose 
            $username = stripslashes( $username );
            $password = stripslashes( $password );

            $username = mysql_escape_string( $username );
            $password = mysql_escape_string( $password );

            //encrypt the password
            $password = md5 ( $password );

            //Establishing Connection with Server by passing server_name, user_id and password as a parameter 
            $connection = mysql_connect( "localhost", "user", "pass");

            //Selecting Database
            $db = mysql_select_db( "database", $connection );

            //SQL query to fetch information of registerd users and finds user match.
            $query = mysql_query( "SELECT * FROM users2 WHERE password='$password' AND username='$username'", $connection );

            //perform query
            $num_rows = mysql_num_rows($query);

            if ($num_rows > 0) {
                header("location: profile.php"); // Redirecting To Other Page
}
else{
     $error = "nope";
}
//if ($rows == 1) {
//   $_SESSION['username']=$username; // Initializing Session
//} else {
//}
            //Closing Connection
            mysql_close( $connection );  
        }
    }
?>
carmine
  • 1,597
  • 2
  • 24
  • 33
  • Every time you use the [mysql_ database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), **a Kitten is strangled somewhere in the world**. It is deprecated (gone for ever in PHP7) If you are just learning PHP, spend your energies learning the `PDO` or `mysqli_ ` database extensions - Otherwise update your code to the correct standard – Takarii Jan 26 '17 at 10:16
  • changed but problem with nothing in if($rows_returned > 0){ loading is still there – Naomi Williams Jan 26 '17 at 14:48

2 Answers2

0

firstly you should write this code top on in code $connection = mysql_connect( "localhost", "user", "pass");

        //Selecting Database
        $db = mysql_select_db( "database", $connection );
lakshmankashyap
  • 437
  • 1
  • 5
  • 14
  • I swapped it to top instead of underneither and got these errors: Warning: mysql_select_db() expects parameter 2 to be resource, null given in /home/ct6014williams/public_html/pages-multi-page/test/login.php on line 39 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/ct6014williams/public_html/pages-multi-page/test/login.php on line 54 – Naomi Williams Jan 26 '17 at 07:13
  • Besides, don't I need to establish the connection before selecting the database? – Naomi Williams Jan 26 '17 at 07:14
0

Just Remove include( 'login.php' ); from index.php because login.php is already loaded with index.php and found that no post request has been made.

And then modify your form action with

<form action="" method="post">

to

<form action="login.php" method="post">
Aditya Raj
  • 168
  • 3
  • 9
  • Thank you for the reply :) I have done these changes but still nothing under if ($num_rows > 0) { will load. – Naomi Williams Jan 26 '17 at 07:51
  • If your php version is bigger than or equal to 5 then you have to use mysqli instead of mysql. And remove $db variable and let only be mysql_select_db( "database", $connection ); and make sure num rows is bigger than 0 by writing this code `print_r($num_rows)` after assigning variable $num_rows. Link for mysqli - http://www.w3schools.com/php/php_ref_mysqli.asp – Aditya Raj Jan 26 '17 at 10:48
  • Done changes as you said but I get many errors of mysqli_escape_string() expects parameter 1 to be mysqli, null given – Naomi Williams Jan 26 '17 at 14:13
  • Use it like this `mysqli_real_escape_string($connection, $username);` , it require db connection also and `mysqli_escape_string` is an alias of `mysqli_real_escape_string` so you may need to change it to that. – Aditya Raj Jan 26 '17 at 14:21
  • Changed but still got error so I put $connection = mysql_connect( "localhost", "user", "pass"); above it and no more error... however the old problem still exists, anything in if($rows_returned > 0){ still does nothing – Naomi Williams Jan 26 '17 at 14:45
  • with print_r($num_rows) the result prints 0 – Naomi Williams Jan 26 '17 at 14:57
  • if $num_rows prints 0 then header location will not work, because $num_rows is not bigger than 0 and Are you sure your database password is "pass" ? because in localhost by default it's usually empty. And give the full error that you are getting. – Aditya Raj Jan 26 '17 at 16:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134098/discussion-between-aditya-raj-and-naomi-williams). – Aditya Raj Jan 26 '17 at 16:07
  • It's okay, I ended up using a different login code than this one as I couldn't get that last part to work. Not the best as I really should have continued with this code to learn where I went wrong but I am on an assignment deadline :) Thank you for all of your help though! – Naomi Williams Jan 26 '17 at 17:36
  • You are welcome, i think you are working on sort of social network type website. Here's my complete social network code written in php for backend on github - https://github.com/adit50/Food-Social-Network-PHP/tree/master/BackEnd checkout if needed. – Aditya Raj Jan 27 '17 at 03:53
  • Thank you :) I'll take a look. I don't suppose you know how to get google maps with geolocation to work with jquery mobile? I'm having a tough time figuring it out :( – Naomi Williams Jan 27 '17 at 06:47
  • I know every thing ;) First get Google Map key from here - https://developers.google.com/maps/documentation/javascript/ (scroll a bit to see the button called "GET A KEY" then click and create new project after that you will get your api key). I have written a easy code for you here - https://jsfiddle.net/adit50/da6zw5c4/3/ (You can copy and paste this code and ya It will not work in JsFiddle website as I have not shared my google maps api key openly in script tag). Connect with me in whatsapp, fb, anything you like, I am happy to help you more. – Aditya Raj Jan 27 '17 at 08:41
  • Sure :) That would be cool! I've used the geolocation code here https://developers.google.com/maps/documentation/javascript/examples/map-geolocation but when I try and add the marker from here https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple it stops working :S Do you know why? – Naomi Williams Jan 27 '17 at 08:51
  • It's actually infoWindow which is not a marker, it's a white box type pointed to map location with some message. Use this code Simple and easy from here - https://developers.google.com/maps/documentation/javascript/examples/marker-simple message me in my whatsapp no - +918603407001 – Aditya Raj Jan 27 '17 at 10:25