-2

Hello everyone first of all I have done a lot of research in the forum and other pages during these whole weekend before finally giving up and posting it.

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>El primer script</title>

    </head>

    <body>
        <div id="wrapper">
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <p>numero: <input type="text" name="number" /></p>
                <p><input type="submit" value="start" /></p>
            </form>
        </div>
        <?php

            if(!isset($_SESSION['CREATED'])) {
                session_start();
                $_SESSION['aux'] = 1;

            } else {
                $_SESSION['aux'] = 0;
            }

            if(isset($_SESSION['CREATED'])){
                var_dump($_SESSION['aux']);
            }       
        ?>
    </body>

</html>

What I want to accomplish is that when I press the button (doesn’t matter if input is empty or not) it shows the number 1 but if I press again doesn't matter how many times it shows 0.

It is not working, it always shows 1, I don't think i managed to make the session work at all.

I might have mistaken the functionality of something rather than making a syntax error. I am beginning with php so if anyone can help if would be appreciated.

Jason
  • 15,017
  • 23
  • 85
  • 116
Rddevelop
  • 133
  • 1
  • 9
  • you called session_start() after you tested the condition if there is a session defined, just put before calling the if statement and that's it – DomainFlag Dec 03 '17 at 23:33
  • 2
    Its not going to show anything, `$_SESSION['CREATED']` never gets set. – Lawrence Cherone Dec 03 '17 at 23:36
  • 1
    Lawrence is right, put session_start() before the if's condition, and initialize $_SESSION["Created"] with a boolean true or whatever value you want in the first statement of the first if condition. – DomainFlag Dec 03 '17 at 23:42
  • i understand that is not defined but if i initializate it in the first if statement it wont make a difference right? it should be initializate before the if shouldnt it? – Rddevelop Dec 03 '17 at 23:44
  • nevermind you are right, it its not defined it means it will enter in the if the initializate it and then never enters again because its defined, thanks to you and lawrence for pointing that out – Rddevelop Dec 03 '17 at 23:46
  • You initialize it before the if statement, you are overriding each time you request the page from the server which is not quite nice although it works for now, but when you'll want to save the changes(overriding and so on, new info) for the $_SESSION["CREATED"] for next time, problems will appear. So, it's a very good practice to test if the session is initialized with the isset function and do other stuff, and if it's not initalized initialize and do other stuff. – DomainFlag Dec 03 '17 at 23:50
  • Sure, i understand, thanks for helping mate. Quite unfortunate that i get downvoted but i struggled with this for 2 days obviously switching to other stuff to relax, but i finally got it so if anyone wants to downvote whatever. I did research a lot before this and when someone adviced to put it as the first element i though it was in the php section.I finally understood it now because he actually showed me the code with the "session_start()" code at the very very first of the file. – Rddevelop Dec 03 '17 at 23:51
  • 1
    if you had error display on, which you should when developing, you would of got an error that would of helped solved this days ago –  Dec 03 '17 at 23:59

1 Answers1

0

Whenever you use the global $_SESSION you have to declare session_start() at the very top of the page.

Try Using:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>El primer script</title>

</head>

<body>
    <div id="wrapper">
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <p>numero: <input type="text" name="number" /></p>
            <p><input type="submit" value="start" /></p>
        </form>
    </div>
    <?php

        if(!isset($_SESSION['CREATED'])) {
            $_SESSION['aux'] = 1;

        } else {
            $_SESSION['aux'] = 0;
        }

        if($_SESSION['CREATED'])){
            var_dump($_SESSION['aux']);
        }       
    ?>
</body>