Note that there are some explanatory texts on larger screens.

plurals
  1. POShould start_session() be always placed at the beginning of the page and not inside an if/else clause?
    primarykey
    data
    text
    <p>This is the <code>index.php</code> file of my website.</p> <pre><code>&lt;?php $jump=false; if (isset($_GET['pg'])) if ($_GET['pg']=="signup") { include 'signup.php'; $jump=true; } if (!$jump) { session_start(); //Has the session been started? If not, if ( isset($_SESSION['myusername']) ) { include 'main.php'; } else { include 'login.php'; } } ?&gt; </code></pre> <p>The first time you enter the site, it executes the <code>include 'login.php;</code>, and takes you to <strong>login.php</strong>. There, if you successfully log in, it sets <code>$_SESSION['myusername']="foo"</code> and redirects you to <strong>index.php</strong>, where if evaluates <code>if ( $_SESSION['myusername']) )</code> as <strong>true</strong> and includes <strong>main.php</strong>. Well, the code works fine in local server <em>but</em> in the Remote Server, the <code>if ( $_SESSION['myusername']) )</code> evaluates <strong>false</strong> and it never includes <strong>main.php</strong>.</p> <p>After some time, I've slightly changed the code, advancing the execution of <code>session_start()</code>:</p> <pre><code>&lt;?php session_start(); if (isset($_SESSION['myusername'])) $session = true; else $session=false; $jump=false; if (isset($_GET['pg'])) if ($_GET['pg']=="signup") { include 'signup.php'; $jump=true; } if (!$jump) { //Has the session been started? If not, if ( $session ) { include 'main.php'; } else { include 'login.php'; } } ?&gt; </code></pre> <p>So the question is: Should I always execute session_start() at the beginning of the code and avoid executing it inside (nested) if-else/while clauses? Why does it work in the local server but fails to carry on in the remote server?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload