-3

Firstly I like to say this is not a duplicate I tried everything on google such as ob_functions and space not luck PHP should of made this simple but they haven't which is stupid..

Why am I getting this error for just starting a cookie in my script? I have commented the cookie start I am having the issue with and hope someone can help fix it I tried and now gave up wasting my time..

<!DOCTYPE html>
<?php 
session_start();

?>


<html lang="en" class="no-js">

    <head>

        <meta charset="utf-8">
        <title>Welcome To Spud Gaming</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <!-- CSS -->
        <link rel='stylesheet' href='http://fonts.googleapis.com/css?family=PT+Sans:400,700'>
        <link rel="stylesheet" href="assets/css/reset.css">
        <link rel="stylesheet" href="assets/css/supersized.css?vvssv">
        <link rel="stylesheet" href="assets/css/style.css?v12">

        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

    </head>

    <body>

<?php
    require "../core/links.php";
    if(isset($_SESSION['user'])) {
    die;

    }

    if(isset($_COOKIE['user'])) {
    die;

    }
    ?>


        <div class="page-container">

        <br><br><br>
            <h1>Sign In</h1>
            <div style='color:black;'>

            <?php 




            if((isset($_POST['username'])) && (isset($_POST['password']))) {
            require "../core/data_connect.php";

            $username = mysqli_real_escape_string($connection,$_POST['username']);
            $password = mysqli_real_escape_string($connection,(sha1(md5($_POST['password']))));

            if(($username) && ($password)){

            $login_query_string = "SELECT * FROM spud_members WHERE ( username='$username' OR email = '$username') and password='$password'";
            $login_query = mysqli_query($connection,$login_query_string);
            $login_rows = mysqli_num_rows($login_query);

            $login_array = mysqli_fetch_array($login_query);
            $mid_login = $login_array['mid'];
            $active_login = $login_array['active'];



        //if we found a user with that information then sign them in
            if($login_rows==1) {

            //if the user as not been suspended then start there session 
            if($active_login==1) {

                //if the user has not checked the remember me box
            if($_POST['start_cookie_true_checked']!='on') {
            $_SESSION['user']=$mid_login;
            die;

            } else if($_POST['start_cookie_true_checked']=='on') {
            //otherwise if the remember me box has been checked then start a cookie instead
            $date_of_expiry = time() + 60 ;

            //THIS COOKIE HAS A ISSUE AND POPS UP THESE ERRORS!!!!!!!!!!!!!!!!!!!!!!!!!
        setcookie("user", 'Mark', time()+3600);


            die;
                    }

            } else {
            echo "<b>Account Suspended</b><br>
                    Your Account Has Been Suspended:<br>
                    This May Be Because You Have Broken Our Terms Of Use Guidelines<br><br>";
                    }


            } else {

            echo "<b>Invalid Username/Email Or Password</b><br>
                    The Username/Email Or Password That You Supplied Is Invalid<br>
                    Please Try Again or <a href=''> Reset Your Password</a>";

            }

            } else {

            echo "<b>Blank Username/Email And Password</b><br>
                    You Haven't Entered A Username/Email And Password";

            }

            }
            ?>

            </div>

            <form action='' method='post'>
            <input type='text' name='username' placeholder='Username Or Email' size='25' maxlength='30' /><br />
            <input type='password' name='password' placeholder='Password' size='25' />


            <input type='checkbox' name='start_cookie_true_checked'>


            <input type='hidden' name='action' value='do_login'>
            <input type='hidden' name='url' value='index.php' /> <br><br>

            <style>
            a {
            color:blue;
            text-decoration:none;
            }
            </style>


            <div>
            <a href='signup'> Need An Account? </a> | <a href='forgot'> Forgot Password?</a>
            </div>

                <button type="submit">Sign In</button>

                    </form>












        </div>

        <!-- Javascript -->
        <script src="assets/js/jquery-1.8.2.min.js"></script>
        <script src="assets/js/supersized.3.2.7.min.js"></script>
        <script src="assets/js/supersized-init.js?ddsddfd"></script>
        <script src="assets/js/scripts.js"></script>

    </body>

</html>

Thanks :)

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
SpudGo
  • 1

2 Answers2

0

Move HTML outpout after to session_start function.

<?php 
session_start();
?>

<!DOCTYPE html>
pavel
  • 26,538
  • 10
  • 45
  • 61
0

You can't try to set a cookie (which is done in response headers) after you have started to make output to the page (which starts on your first line of code).

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • it does work in my first PHP tag after the session_start so guess your right so try to re-structure the code. it's stupid how php is built like that – SpudGo Jan 27 '15 at 02:56
  • @SpudGo not really a PHP problem. This is how HTTP protocol works. It might be more of a case that due to the popularity and the huge number of not so good resources on PHP out there, that this not not always made clear. – Mike Brant Jan 27 '15 at 03:39
  • Yeah Agree With That I have put the code up the top before any HTML code is rendered that has solved the problem. so guess PHP should be rendered first then HTML for some functions to work And thanks again ;) – SpudGo Jan 28 '15 at 09:00