Note that there are some explanatory texts on larger screens.

plurals
  1. POFun with sessions
    text
    copied!<p>I've a problem that annoy me very much. It's because I'm trying to make a PHP login script. But when I log in correctly, it'll not let me in.</p> <p>If I comment out some lines (I'll mark them), the script works, but that's the code I was planning to use to check in on every page, so people don't can come in if they don't should have access.</p> <p>Below, I've posted the code.</p> <hr> <h2>index.php</h2> <pre><code>&lt;?php function __autoload($class_name) { require_once "./functions/" . $class_name . ".php"; } $functions = new functions; $functions-&gt;header("Log ind",0); $login = new login; $login-&gt;showLogin(); $functions-&gt;footer(); ?&gt; </code></pre> <hr> <h2>/functions/functions.php</h2> <pre><code>&lt;?php // Define class functions class functions { function header($titel,$needlogin = 1) { session_start(); echo $_SESSION['navn']; // The following lines can be commented out, and it's working if($needlogin == 1) { if(!isset($_SESSION['id'])) { header("Location: http://hansensopskrifter.co.cc/"); exit; } } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" &gt; &lt;head&gt; &lt;title&gt;&lt;?php echo $titel; ?&gt; - Hansens Opskrifter&lt;/title&gt; &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hansens Opskrifter&lt;/h1&gt; &lt;?php // The above lines can be commented out, and it's working } function footer() { ?&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } } ?&gt; </code></pre> <hr> <h2>/functions/mysql.php</h2> <pre><code>&lt;?php // Create the class MySQL class mysql { function __construct() { $this-&gt;mysqlconnect(); } function mysqlconnect() { $conn = mysql_connect("localhost","user","pass"); if(!$conn) { die("Noget gik galt - kontakt Kristoffer og vis ham den følgende meddelelse: " . mysql_error()); } if(!mysql_select_db("db",$conn)) { die("Noget gik galt - kontakt Kristoffer og vis ham den følgende meddelelse: " . mysql_error()); } } function mysqlquery($query) { $result = mysql_query($query); if($result) { return $result; } } } ?&gt; </code></pre> <hr> <h2>login.php</h2> <pre><code>&lt;?php function __autoload($class_name) { require_once "./functions/" . $class_name . ".php"; } $name = $_REQUEST['name']; $pass = $_REQUEST['pass']; $login = new login; $l = $login-&gt;doLogin($name,$pass); if($l == TRUE) { header("Location: http://hansensopskrifter.co.cc/loggedin.php"); } else { exit; } ?&gt; </code></pre> <hr> <h2>/functions/login.php</h2> <pre><code>&lt;?php class login { function __autoload($class_name) { require_once($class_name . ".php"); } function showLogin() { ?&gt; &lt;h2&gt;Log ind&lt;/h2&gt; &lt;form action="./login.php" method="post"&gt; Navn:&lt;input type="text" name="name" /&gt; Kode:&lt;input type="password" name="pass" /&gt; &lt;input type="submit" value="Log ind" /&gt; &lt;/form&gt; &lt;p&gt;&lt;a href="./forgotpass.php" alt="Glemt kode" title="Glemt kode"&gt;Glemt kode?&lt;/a&gt;&lt;/p&gt; &lt;?php } function doLogin($name,$pass) { $mysql = new mysql; $n = mysql_real_escape_string($name); if(!$n) { $functions = new functions; $functions-&gt;header("Intet navn indtastet",0); echo "Du glemte at indtaste dit navn."; $this-&gt;showLogin(); $functions-&gt;footer(); return false; } elseif(!$pass) { $functions = new functions; $functions-&gt;header("Ingen adgangskode indtastet",0); echo "Du glemte at indtaste din adgangskode."; $this-&gt;showLogin(); $functions-&gt;footer(); } $query = "SELECT `id`, `navn`, `kode` FROM `users` WHERE `navn` = '".$n."' "; $result = $mysql-&gt;mysqlquery($query); while($row = mysql_fetch_assoc($result)) { $k = sha1($pass); $navn = $row['navn']; $kode = $row['kode']; $n = ucfirst(strtolower($n)); if($navn == $n &amp;&amp; $kode == $k) { $_SESSION['id'] = $row['id']; $_SESSION['navn'] = $row['navn']; return true; } else { $functions = new functions; $functions-&gt;header("Forkert navn eller kode",0); echo "Det indtastede navn eller kode er forkert."; $this-&gt;showLogin(); $functions-&gt;footer(); return false; } } } } ?&gt; </code></pre> <hr> <h2>loggedin.php</h2> <pre><code>&lt;?php function __autoload($class_name) { require_once ("./functions/" . $class_name . ".php"); } header( "refresh:2;url=./panel/index.php",0); $functions = new functions; $functions-&gt;header("Logger ind..."); ?&gt; &lt;p&gt;Du er nu logget ind. Du vil automatisk blive viderestillet om omkring 5 sekunder. Hvis du er tr&amp;aelig;t af at vente kan du &lt;a href="./panel/index.php" alt="Opskrifter" title="Opskrifter"&gt;klikke her&lt;/a&gt;.&lt;/p&gt; &lt;?php $functions-&gt;footer(); ?&gt; </code></pre> <hr> <p>I've tried a lot of things, and now, I just hope you can help me. I've checked that there is a cookie that's created called PHPSESSID on my computer.</p> <p>Thank you very much in advance.</p>
 

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