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

Issue with cookies and mysql.

I have flowing log in system:

<?php
$section = "reseller";
include('inc/header.php'); 
$login = "";
$logged = "0";
$warning = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    require("include/config.php");
    $userpw = mysql_real_escape_string(md5($_POST['pw']));
    $userid = mysql_real_escape_string($_POST['email']);

    $query = "SELECT * FROM `reseller` WHERE `email`='$userid' AND `pass`='$userpw'";
    $result = mysql_query($query);
    $resultcount = mysql_num_rows($result);
    if ($resultcount == 0) {
        $warning = "Wrong username or password";
    } else {
        setcookie("user", $userid, time()+3600);
        setcookie("pass", $userpw, time()+3600);
        $logged = "1";

        }
    }

?>


    <div id="wrapper">
      <section id="primary">
        <h3>Reseller section</h3>     
          <fieldset>

              <?php 

                if (isset($_COOKIE["user"])) {

                    $userpw = mysql_real_escape_string($_COOKIE["pass"]);
                    $userid = mysql_real_escape_string($_COOKIE["user"]);  

                    $query = "SELECT * FROM `reseller` WHERE `email`='$userid' AND `pass`='$userpw'";
                    $result = mysql_query($query);
                    $resultcount = mysql_num_rows($result);
                    if ($resultcount == 0) {
                        $warning = "Wrong username or password!";
                    } else {
                        while($row = mysql_fetch_object($result)){
                            $valid = $row->valid;
                            $age = $row->birthday;
                            $price = $row->price;
                            $email = $row->email;
                            $name = $row->name;
                            $id = $row->id;

                            If ($valid == "true") {
                                $valida = "Yes"; 
                            } elseif ( $valid == "pending") {             
                                $valida = "Is being reviewed";
                            } else {
                                $valida = "No"; 
                            }
                        }
                    }                    
              ?>

              <p><?php echo $warning ?></p>
                <ul>
                    <li>
                        <a href="login.php">Profile Picture</a>
                    </li>
                    <li>
                        <a href="login.php">Set name</a>
                    </li>
                    <li>
                        <a href="login.php">Set age</a>
                    </li>
                    <li>
                        <a href="login.php">Set price</a>
                    </li>
                    <li>
                        <a href="login.php">Set email</a>
                    </li>
                    <li>
                        <a href="login.php">Delete Account</a>
                    </li>


                </ul>
                <?php 

              } else {

                ?>
                    <legend>Login</legend>
                        <form method="POST" action="#">
                        <p><?php echo $warning ?></p>
                        <label for="email">Email:<br></label>
                        <input type="text" name="email" id="email"><br>
                        <label for="password">Passwort:<br></label>
                        <input type="password" name="pw" id="pw"><br><br>
                        <input type="submit" value="Login"> or sign up <a href="register.php">here</a>.
                    </form>
                <?php
              }

                  ?>

          </fieldset>

    </section>
</div>

<?php include('inc/footer.php'); ?>

My problem is: If i fill in username and password and click on "Login". I get redirected to the log in form a second time. But i should be redirected to the profile page.

2 Answers

Here the same code in pastebin (for better formatting): http://pastebin.com/NLa25Mw6

From what I see. You might want to add a redirection => "header('Location: filename.php'); " in your php after authenticating it and validating it.

Ex. if ($_SERVER["REQUEST_METHOD"] == "POST") { ...... } else { setcookie("user", $userid, time()+3600); setcookie("pass", $userpw, time()+3600); $logged = "1";

   //add redirect location =>       header("location: profile.php?$userid");

      }
}

Hopefully that might work :).