Kiro Kiro - 4 months ago 12
PHP Question

How to keep the user logged on the entire web

I have a problem logging users. When logs, session work, but when you click to another page, the session will disappear and does not work and when I put session_start to the main page and to login.php, I got error that session is already running. Some ideas? Thanks

Main Page.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="css.css">
<link rel="icon" href="favicon.ico">
<title>----</title>
</head>
<body>

<!-- HEADER _____________________________________________________________________________ -- >
<div id="whiteblock">
<div id="grayblock">
<div id="logo">
</div>
<h1>----</h1>
<h2>----</h2>

<a href="websiteregister.php" id="registration">Registration</a>

<?php
include "Login.php";
echo $_SESSION["LogiNick"];
?>

<div id="inputposunuti">
<form name="Log" id="Log" method="post">
<label for="Nick" class="inputtext">Nick:</label>
<input type="text" name="LogiNick" id="LogiNick" class="input"> <?php echo $EmptyNick; echo $Else; ?>

<label for="Pass" class="inputtext">Password:</label>
<input type="password" name="LogiPass" id="LogiPass" class="input"> <?php echo $EmptyPass; echo $Else; ?>

<input type="submit" name="LogIn" value="⇒">
</form>

</div>
</div>
<!-- HEADER _____________________________________________________________________________ -->

<!-- MENU _______________________________________________________________________________ -->

<div id="pruh">
<div id="search">
<form name="search" method="post">
<input type="text" name="hledat" id="sirka">
<input type="submit" name="subhledat" value="Search!" id="button">
</form>
</div>
<div id="menutext">
<a href="website.php"> <p>Home</p> </a>
<a href="#"> <p>----</p> </a>
<a href="#"> <p>----</p> </a>
<a href="#"> <p>----</p> </a>
<a href="#"> <p>----</p> </a>
<a href="#"> <p>----</p> </a>
</div>
</div>

<!-- MENU _______________________________________________________________________________ -->

<!-- CONTENT ____________________________________________________________________________ -->

<!-- CONTENT ____________________________________________________________________________ -->

<div id="teams"></div>
</div>


<p id="creator">Created by</p>





Login.php

<?php
session_start();

include_once "db.php";
global $db;

$Else = $EmptyNick = $EmptyPass = $EmptyNick = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$LogiNick = $_POST["LogiNick"];
$LogiPass = $_POST["LogiPass"];

if (empty($_POST["LogiNick"])) {
$EmptyNick = " - Nick";
}
if (empty($_POST["LogiPass"])) {
$EmptyPass = " - Password";
}


}

if(!empty($_POST["LogiNick"]) AND !empty($_POST["LogiPass"])){

$LogiPass = $_POST["LogiPass"];
$SHA = sha1($LogiPass);

$sql = "SELECT * FROM WEB_REGISTER WHERE Nick = :nick AND Heslo = :heslo";
$query = $db->prepare($sql);
$query->execute(array('nick' => $LogiNick, 'heslo' => $SHA));

$count = $query->rowCount();

if ($count > 0) {
$_SESSION["LogiNick"];
}
}
?>

Answer

You just set a session if you put a value into it.

$_SESSION["name"]=$value;