Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Patrick Karkafi
Patrick Karkafi
508 Points

Php Header not redirecting

Hello, in my code, after logging or signing up in you will get redirected to the profile page. Unfortunately, when i switched to website hosting instead of localhost, the iframe at the top of page is updating but the page is not redirecting to the profile page. Here is my code for the login.php that is set to go when someone presses log in:

(no whitespace here, not the source of the problem)

<?php
session_start();
// Escape email to protect against SQL injections
$email = $mysqli->escape_string($_POST['email']);
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'");

if ( $result->num_rows == 0 ){ // User doesn't exist
    $_SESSION['message'] = "User with that email doesn't exist!";
   header(:'Location: backerror.php');
}

else { // User exists
    $user = $result->fetch_assoc();

    if ( password_verify($_POST['password'], $user['password']) ) {

        $_SESSION['email'] = $user['email'];
        $_SESSION['first_name'] = $user['first_name'];
        $_SESSION['last_name'] = $user['last_name'];
        $_SESSION['active'] = $user['active'];
        $_SESSION['cash_amount'] = $user['cash_amount'];
        $_SESSION['active_skin'] = $user['active_skin'];
        $_SESSION['games_played'] = $user['games_played'];
        $_SESSION['money_made'] = $user['money_made'];
        $_SESSION['money_lost'] = $user['money_lost'];
        $_SESSION['id'] = $user['id'];

        // This is how we'll know the user is logged in
        $_SESSION['logged_in'] = true;

        header('Location: profile.php');
    }
    else {
        $_SESSION['message'] = "incorrect password, please try again!";
        header('Location: backerror.php');
    }
}

And here is my code for the php page that calls the php file:

<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];

if(preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))

header('Location: ../mobiledetect.php'); ?>
<?php
/* Main page with two forms: sign up and log in */
require 'db.php';
session_start();
?>
  <!DOCTYPE html>
  <html style="max-width: 100%; overflow-x: hidden;">

  <head>
    <title>Sign-Up/Login</title>
    <?php include 'css/css.html'; ?>
    <script src="https://use.fontawesome.com/419fbde60c.js"></script>
  </head>

  <?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (isset($_POST['login'])) { //user logging in

        require 'login.php';

    }

    elseif (isset($_POST['register'])) { //user registering

        require 'register.php';

    }
}
?>

    <body style="background-color: #1c1d26; max-width: 100%; overflow-x: hidden;">
      <div
   style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;"
   unselectable="on"
   onselectstart="return false;">


<iframe id="section" src="../navbar.php" style="width: 100%; height: 75px; border: none; padding: 0px; position: relative; left: 0%; top: 0%;" scrolling="no"></iframe>

      <div class="form" style="position: relative; top: 30px;">

        <ul class="tab-group">
          <li class="tab"><a href="#signup">Sign Up</a></li>
          <li class="tab active"><a href="#login">Log In</a></li>
        </ul>

        <div class="tab-content">

          <div id="login">
            <h1>Welcome Back!</h1>

            <form action="loginpage.php" method="post" autocomplete="off">

              <div class="field-wrap">
                <label style="pointer-events: none;">
              Email Address<span class="req">*</span>
            </label>
                <input type="email" required autocomplete="off" name="email" />
              </div>

              <div class="field-wrap">
                <label style="pointer-events: none;">
              Password<span class="req">*</span>
            </label>
                <input type="password" required autocomplete="off" name="password" />
              </div>

              <p class="forgot"><a href="forgot.php">Forgot Password?</a></p>

              <button class="button button-block" name="login" />Log In</button>

            </form>

          </div>

          <div id="signup">
            <h1>Sign Up for Free</h1>

            <form action="loginpage.php" method="post" autocomplete="off">

              <div class="top-row">
                <div class="field-wrap">
                  <label style="pointer-events: none;">
                First Name<span class="req">*</span>
              </label>
                  <input type="text" required autocomplete="off" name='firstname' />
                </div>

                <div class="field-wrap">
                  <label style="pointer-events: none;">
                Last Name<span class="req">*</span>
              </label>
                  <input type="text" required autocomplete="off" name='lastname' />
                </div>
              </div>

              <div class="field-wrap">
                <label style="pointer-events: none;">
            Email Address<span class="req">*</span>
            </label>
                <input type="email" required autocomplete="off" name='email' />
              </div>

              <div class="field-wrap">
                <label style="pointer-events: none;">
            Set A Password<span class="req">*</span>
            </label>
                <input type="password" required autocomplete="off" name='password' />
              </div>


              <div class="field-wrap" style="position: relative; top: -10px;">
                <input type="checkbox" id="terms" name="terms" required>
                <label for="terms"><a style="font-size: 23px; color: white; position: relative; left: -13px;">I Accept the <a style="text-decoration: underline; font-size: 23px; position: relative; left: -13px;" href="../terms.html" target="_blank">Terms and Conditions</a></a><span class="req" style="position: relative; left: -13px; font-size: 23px;">*</span> </label>
              </div>



              <button type="submit" class="button button-block" name="register" style="cursor: pointer;" />Register</button>

            </form>

          </div>

        </div>
        <!-- tab-content -->

      </div>
      <!-- /form -->
<br>
<br>
<br>

<div style="position: relative; margin-bottom: -210px">
<iframe id="section" src="../footer.php" style="width: 100%; min-width: 750px; height: 210px; border: none; position: relative; margin-left: 0px; " scrolling="no"></iframe>
</div>

      <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

      <script src="js/index.js"></script>

</div>
    </body>

  </html>

I didn't add the signup.php code because i am guessing the source of the problem is the same for both actions.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Have you tried a fully qualified url like

<?php 
header('Location: http://yourdomain.com/profile.php');
?>
Patrick Karkafi
Patrick Karkafi
508 Points

Yes i tried the full url but it responds with a blank white screen.

Patrick Karkafi
Patrick Karkafi
508 Points

Oh nevermind, that was my fault. There is no blank screen it produces the same result, no redirect.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Do you have error_reporting set to E_ALL and display_errors set to ON so that php detected errors would be reported and displayed. Also have you tried the php headers_sent() function.

2 Answers

In your first code snippet you have:

<?php
header(:'Location: backerror.php');
?>

There is a colon before the Location string. That needs to be removed.

Patrick Karkafi
Patrick Karkafi
508 Points

Ok guys i still dont know the problem but thanks for all your help. I changed the redirects to be sent by javascript and that fixed it. The error must have been really deep in for me to see it.