0
package loginphp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Loginphp {

    public static void main(String[] args) throws MalformedURLException, IOException {
        String url = "http://localhost/login/login.php";
        String Parameters = "username=admin&password=admin";
        byte[] postdata = Parameters.getBytes("UTF-8");
        String useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0";
        URL urlLink = new URL(url);

        HttpURLConnection urlConn = (HttpURLConnection) urlLink.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setRequestProperty("User-Agent", useragent);
       urlConn.setRequestProperty("Content-Length", String.valueOf(postdata.length));
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConn.setInstanceFollowRedirects(true);
        urlConn.setDoOutput(true);
        urlConn.getOutputStream().write(postdata);

        System.out.println("Connecting to " + url);
        System.out.println("Response " + urlConn.getResponseMessage());
        System.out.println("Code " + urlConn.getResponseCode());

        InputStream is = urlConn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader bis = new BufferedReader(isr);

        String output;

        while ((output = bis.readLine()) != null) {

            System.out.println(" " + output);
        }

    }

}

Am sending a post request to the following code, but its not redirecting to welcome page after login, Am not sure if its even logging in. The result am getting is the same code for logging page. What could be the problem ? My main goal is to logging and redirect to welcome.php. The script is working perfect using browser. Am able to logging, get redirected to welcome page and logout.

<?php
   include("config.php");
   session_start();
   $error = "";
   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
        // session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>

   <head>
      <title>Login Page</title>

      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }

         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }

         .box {
            border:#666666 solid 1px;
         }
      </style>

   </head>

   <body bgcolor = "#FFFFFF">

      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

            <div style = "margin:30px">

               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>

               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

            </div>

         </div>

      </div>

   </body>
</html>
  • You are not actually redirecting anything. The `Location` header is one part of two items you need to redirect. You are trying to do a 303 redirect. – Philip Couling Mar 19 '18 at 17:16
  • Possible duplicate of [How to implement 303 redirect?](https://stackoverflow.com/questions/13003192/how-to-implement-303-redirect) – Philip Couling Mar 19 '18 at 17:16
  • their is no answer that is helping among the three. Be straight to the point. I want to get the welcome page code using java code above am trying to login , after i login i get this code as output Welcome

    Welcome

    Sign Out

    – webscrapingtech Mar 19 '18 at 17:39
  • This is what you already get? or what you want to get? If you are getting this now then something may be misconfigured in your webserver because nothing should be able to see the `` tags outside the webserver. – Philip Couling Mar 20 '18 at 10:02

1 Answers1

0

The short answer is You should use a 303 redirect:

header("HTTP/1.1 303 See Other");
header("location: welcome.php");
Philip Couling
  • 13,581
  • 5
  • 53
  • 85