I just copied your code and tested it out myself. Everything is good but do not use, session_name('test') in your access.php file. Not really sure what that does but it breaks when I have it included. Instead, I used the $_SESSION['name'] without calling the session_name() function and all is working.
PHP sessions without cookies
-
12-12-2021 - |
Frage
i'm a little lost working without cookies. I want to create a session passing the SID through the url, but i don't know how to pass and get data from another page. I've googled a lot but 90% of the examples are with cookies.
Here is what i have.
index.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
?>
<html>
<head>
<title>Index</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>
<a href="index.php?" class="navactive">Index</a>
<a href="second.php?">Second</a>
<form action="access.php" method="POST">
User: <input type="text" name="user" size="15"/><br>
Pass: <input type="password" name="pass" size="15"/><br>
<button type="submit"/>Ok</button>
<button type="reset"/>Reset</button>
</form>
Logged as: <?php print $_SESSION["name"]; ?>
</body>
</html>
access.php last part
........
.......
....
if($count==1){
// Register $myusername and redirect to file "second.php"
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_name('test');
session_start();
$_SESSION['name'] = $myusername;
header("location:second.php?".SID);
exit;
}
else {
echo "Wrong Username";
}
ob_end_flush();
?>
second.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
?>
<html>
<head>
<title>Second</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>
<a href="index.php?">Index</a>
<a href="second.php?" class="navactive">Second</a><br>
<a href="logout.php">Logout</a><br>
Logged as: <?php print $_SESSION["name"]; ?>
</body>
</html>
logout.php
<?php
ini_set("session.use_cookies",0);
ini_set("session.use_only_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
session_unset();
session_destroy();
header('Location: index.php');
exit;
?>
-What do i have to put in "Logged as:" ?. "print $_SESSION["name"];" shows nothing.
-When i log in, i'm redirected to second.php, then i click on any link and the actual logged session dies and SID changes.
thx!
Lösung
Andere Tipps
In order to pass anything through a URL you must use the proper syntax:
your.url.com/?key=value&key2=value2... and so on
Then to retrieve this data:
echo $_GET['key']
and echo $_GET['key2']
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow