Skip to content
Advertisement

the session created in a page doesnt shares with other

when i create a session in php it still only in one page but doesnt get shared with others the login page code:

<html lang="en" style='background-color: rgb(175, 192, 175);'>
    <head>
        <title>Kosumi - login</title>
        <link rel = "icon" href = "imglogo.png">
        
    </head>
    <form action="includes/login.inc.php" method="post" style="position:absolute;height: 200px; width: 200px;background-color: rgb(159, 181, 185);left: 0; top: 0; bottom: 0;right: 0; margin: auto;">
        <div style="height: 165px; width: 170px;left: 0; top: 0; bottom: 0;right: 0;margin:auto">
            <a href="index.php" style="font-size: 12px; margin-bottom: 0px;">back</a>
            <p style="margin-top: 2px;">USERNAME</p>
            <input type="text" name="username" id="username" role='input'>
            <p>PASSWORD</p>
            <input type="password" name="password" id="password">
        </div>
        <div style="height: 150px; width: 50px;left: 0; top: 0; bottom: 0;right: 0;margin:auto">
            <button style="right: 0;" id='login'>login</button>
        </div>
    </form>
</html>

my login code:

<?php
session_start();
$_SESSION['user'] = $_POST['username'];
echo $_SESSION['user'];
header("location:../index.php");
?>

and the logoff one:

<?php
session_start();
if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();
header("location:../index.php");
?>

i have noticed that the browser saves a “phpsessid” cookie but it doesnt seems to share with other pages. i tried to print the session id with echo session_id(); but it is just blank.

Advertisement

Answer

the problem in my code is that i should have added session_start(); in other pages that requires the session info too,like in the main page the user is being shown in the main page due to session start

it seems like the page wont load the session info without that function. as andrea meant when asked if i have session_start(); in other pages. i am just answering because i know the solution now and nobody have written it in my post already, so other people can see easily how to solve this, thank you for who helped me with this.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement