Sessione non viene aperta
-
Il problema è che non la usi proprio...
-
-
Si, o in alternativa metterla nel config, in modo che sia valida per tutti.
Il session_start deve comunque stare prima di qualsiasi istruzione di output, altrimenti ti genera un errore. -
Oddio... mi da quest'errore su restricted.php o.O
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\nfs\config.php:5) in C:\xampp\htdocs\nfs\config.php on line 6
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\nfs\config.php:5) in C:\xampp\htdocs\nfs\validator.php on line 3
config.php
<html> <head> </head> <body> <?php session_start(); /* Set your mysql parameters */ $dbhost="localhost"; //host (usually localhost) $dbuser="root"; //mysql username (usually root) $dbpass=""; //mysql password $connection = mysql_connect ("$dbhost", "$dbuser", "$dbpass"); if (!$connection) { die ('Could not connect: ' . mysql_error()); } ?> </body> </html>validator.php
<?php require('config.php'); header('Location: restricted.php'); ?> <html> <head> </head> <body> <?php $uname=$_POST["username"]; $tpass=sha1($_POST["password"]); mysql_select_db("cms", $connection); $query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1"); mysql_query($query); if(mysql_num_rows($query) == 1) { $lin = mysql_fetch_array($query); $_SESSION['login'] = $lin['name']; exit; } else { die('Nome Utente o Password errati'); } ?> </body> </html>restricted.php
<?php require('config.php'); if(!isset($_SESSION['lin'])) { print "note set"; } ?> <head> <title>Area privata</title> </head> <body> Pagina privata!<br /> <br /> <br /> <a href="logout.php">Logout</a><br /> </body> </html> -
COme ti ho già detto prima, il session_start deve essere la prima istruzione del file, tu stai mandando in output <html><body> e dopo apri la sessione...
Comunque se nel config ci metti pure tag html, rimettendoli anche negli altri file, se guardi il sorgente di output finale vedrai molto codice ripetuto...
-
OK ho modificato il config.php togliendo i tag html
config.php:
<?php session_start(); /* Set your mysql parameters */ $dbhost="localhost"; //host (usually localhost) $dbuser="root"; //mysql username (usually root) $dbpass=""; //mysql password $connection = mysql_connect ("$dbhost", "$dbuser", "$dbpass"); if (!$connection) { die ('Could not connect: ' . mysql_error()); } ?>però rimane l'errore:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\nfs\config.php:18) in C:\xampp\htdocs\nfs\validator.php on line 3
in validator.php ho questo sulla terza riga:
header('Location: restricted.php');
-
Metti l'include del config.php come prima riga su tutti i file.
-
-
Si, ma deve essere in cima,perchè stai mandando output prima del session_start...
-
-
Salve, ho risolto il problema riscrivendo il codice così:
<?php session_start();
require('config.php');
?>
<html>
<head>
</head>
<body>
<?php
$uname=$_POST["username"];
$tpass=sha1($_POST["password"]);mysql_select_db("cms", $connection);
$query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");
mysql_query($query);
if(mysql_num_rows($query) == 1)
{
print "Logged in";
$_SESSION['login']=$uname;
}
else
{
die('Nome Utente o Password errati');
}?>
</body>
</html>in poche parole ho messo
$_SESSION['login']=$uname;
al posto di
$_SESSION['login'] = $lin['name'];
lo ammetto... non è il massimo dell'eleganza ma prima per qualche motivo non funzionava!
Secondo voi questa "strategia" ha qualche difetto? -
Ragazzi... ho capito dove avevo sbagliato...

in pratica io mettevo
$query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");
e poi
$_SESSION['login'] = $lin['name'];
però mi basta sostituire name con id che funziona alla grande... comunque grazie a tutti per il vostro aiuto

Ora un dilemma... nella sessione è meglio tenere memorizzato id o nome? Nel dubbio entrambi

Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi