简体   繁体   中英

PHP + MySQL: How to auto login after registration

So what I want to accomplish is that users that registers get logged inn automatically but all i get is a white page and nothing happens... the data is added to the database but the user is not automatically logged in... Here's some code for you

First the form: (if there already is a user logged inn this will not show)

function registrer(){
if(!isset($_SESSION['brukerID'])){
echo'<center><h4>Registrer ny bruker!</h4></center><hr/>
<form method="POST" action="nybruker.php">
Passord<br>
    <input type="password" name="nybrukerpass" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Æ]).{8,}" title="Må bestå av minst 1 tall og en stor bokstav og små bokstaver og minst 8 bokstaver."><br>
Email<br>
    <input type="text" name="nybrukeremail" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" title="Epost@Domene.no"><br>
Fornavn<br>
    <input type="text" name="nybrukerfornavn" required ><br>
Etternavn<br>
    <input type="text" name="nybrukeretternavn" required ><br>
Adresse<br>
    <input type="text" name="nybrukeradresse" required ><br>
    <p>
        <input type="submit" value="Registrer Nå!" name="sendKnapp">
    </p>
</form>';}
}

Now the nybruker.php:

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

$nybrukerpass = $_POST['nybrukerpass'];
$nybrukerfornavn = $_POST['nybrukerfornavn'];
$nybrukeretternavn = $_POST['nybrukeretternavn'];
$nybrukeradresse = $_POST['nybrukeradresse'];
$nybrukeremail = $_POST['nybrukeremail'];

 $sql1 = "INSERT INTO Bruker (BrukerID, Passord, Fornavn, Etternavn, Adresse, Epost, ErAnsatt)
        VALUES('NULL','$nybrukerpass', '$nybrukerfornavn', '$nybrukeretternavn','$nybrukeradresse', '$nybrukeremail', '0')";

 $sql2 = "SELECT BrukerID FROM Bruker WHERE Fornavn = '$nybrukerfornavn' AND Etternavn = '$nybrukeretternavn')";

 $brukerid = $sql2;

if ($sql1 and $sql2) {
    mysql_query("COMMIT");
    $_SESSION['brukerID'] = $brukerid;
    header( 'Location: index.php' );
} else {        
    mysql_query("ROLLBACK");    
    echo "feil i kode";
}

You're setting a session variable, but I don't see you starting a session in your script. Which would cause the problem you're describing where the user doesn't seem to be logged in. Add this to the top of your scripts if it's not already there.

session_start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM