2

I'm struggling with this issue, for some reason header("Location:http://corocloud.com/index.php/"); is not working, i've tried other paths to the file but none work, header("Location:index.php");, header("index.php");, header("./index.php/");

none of these work, my code is this:

<!DOCTYPE html>
<html>
    <head>
      <title>CoroCloud</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <script src="./js/material.min.js"></script>
      <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
      <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.pink-indigo.min.css" />
      <style>.mdl-layout{align-items:center;justify-content:center;}.mdl-layout__content{padding:24px;flex:none;}</style>
  </head>
  <body>
    <div class="mdl-layout mdl-js-layout mdl-color--grey-100">
      <main class="mdl-layout__content">
        <div class="mdl-card mdl-shadow--6dp">
          <div class="mdl-card__title mdl-color--primary mdl-color-text--white">
            <h2 class="mdl-card__title-text">CoroCloud</h2>
          </div>
          <div class="mdl-card__supporting-text">
            <form method="POST">
              <div class="mdl-textfield mdl-js-textfield">
                <input class="mdl-textfield__input" type="text" name="uname" />
                <label class="mdl-textfield__label" for="uname">Username</label>
              </div>
              <div class="mdl-textfield mdl-js-textfield">
                <input class="mdl-textfield__input" type="password" name="pass"/>
                <label class="mdl-textfield__label" for="pass">Password</label>
              </div>
              <div class="mdl-card__actions">
                <input type="submit" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect" name="sub">
              </div>
            </form>
          </div>
        </div>
      </main>
    </div>
    <?php 
      session_start();
       // this will trigger when submit button click
       if(isset($_POST['sub'])){
       
        $db = new mysqli("localhost","user","password","db");
       
        // create query
        $query = "SELECT * FROM users WHERE user='".$_POST['uname']."' AND password='".sha1($_POST['pass'])."'";
       
        // execute query
        $sql = $db->query($query);
        // num_rows will count the affected rows base on your sql query. so $n will return a number base on your query
        $n = $sql->num_rows;
       
        // if $n is > 0 it mean their is an existing record that match base on your query above 
        if($n > 0){
                              $_SESSION['username'] = $_POST['uname'];
                              $_SESSION['password'] = $_POST['pass'];
                              $_SESSION['userobj'] = mysql_fetch_assoc($query);
     header("Location: index.php");
   exit();
        } else {
       
         echo "Incorrect username or password";
        }
       }
      ?>
  </body>
</html>

I know the code is being executed, as the every $_SESSION var is getting value, why does header not work?

The file i'm trying to redirect is in the same folder by the way.

EDIT: Don't run the snippet, as it has PHP

Alex Coronas
  • 468
  • 1
  • 6
  • 21

6 Answers6

4

Add this code at the top of your code // before html code

ob_start();
session_start();
    // this will trigger when submit button click
    if(isset($_POST['sub'])){

        $db = new mysqli("localhost","user","password","db");

        // create query
        $query = "SELECT * FROM users WHERE user='".$_POST['uname']."' AND password='".sha1($_POST['pass'])."'";

        // execute query
        $sql = $db->query($query);
        // num_rows will count the affected rows base on your sql query. so $n will return a number base on your query
        $n = $sql->num_rows;

        // if $n is > 0 it mean their is an existing record that match base on your query above 
        if($n > 0){
                      $_SESSION['username'] = $_POST['uname'];
                      $_SESSION['password'] = $_POST['pass'];
                      $_SESSION['userobj'] = mysql_fetch_assoc($query);
        header("Location: index.php");
        exit();
        } else {

            echo "Incorrect username or password";
        }
    }

// Put here html code //
Devdutt Sharma
  • 391
  • 1
  • 2
  • 10
2

You cannot output stuff and after that perform a "header" operation in php. So, modify your code to first do all the php stuff and after that output html. You MUST not have ANY output (html, warnings, etc.) before that "header" line. see: http://php.net/manual/en/function.header.php

Paladin
  • 1,637
  • 13
  • 28
1

Please try with ob_start() function before and Header Location

ob_start();
header("Location:http://corocloud.com/index.php/");
exit;

I hope it will help ! you please provide the error what you are getting over there.

Devdutt Sharma
  • 391
  • 1
  • 2
  • 10
1

You may not produce output (HTML or echo) before a header is called. http://php.net/manual/en/function.header.php

Vbudo
  • 405
  • 4
  • 9
1

As Icewine pointed out correctly, headers should be set before any output is created.

So either move your code that is setting the header to the beginning of your script, or capture all output with output buffering, by calling ob_start() at the beginning of your script.

Also It is customary to not send any content when redirecting with a Location header, another reason to move your logic for the redirect to the beginning of your script, and then call exit() after setting the header.

Thakkie
  • 604
  • 4
  • 17
  • Thank you! I didn't know that i couldn't output before changing the header. As a mattern of fact i have this same code in a hosting, and it works. That's strange – Alex Coronas Apr 21 '17 at 09:32
0

Better way is always try to follow some php basics.

<?php
ob_start();
session_start();

exit after header

header("Location:demo.php");
exit;
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33