Login system using txt file throws error: localhost redirected you too many times
I've got a php login system on my website that I'm developing offline using XAMPP. Every time I try to use the login system I keep getting this error:
localhost redirected you too many times.
I've tried checking around but can't find any help.
Here's my code in index.php:
<?php
session_start();
$userKey = $_POST['userKeySubmitted'];
$keylist = file('priv/keys.txt');
$success = false;
foreach ($keylist as $key) {
$user_details = explode('|', $user);
if ($user_details[0] == $userKey) {
$success = true;
if ($success) {
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
break;
} else {
break;
}
}
}
?>
....
<form method="post" action="index.php">
<input type="text" name="userKeySubmitted" class="form-control" placeholder="XXXX-XXXX-XXXX-XXXX">
<br>
<button type="submit" name="submit" class="btn btn-info btn-fill pull-right">Login</button>
<div class="clearfix"></div>
</form>
In user.php:
<?php
session_start();
if (isset($_SESSION['userKey'])) {
$key = $_SESSION['userKey'];
echo "<h1>Welcome</h1>";
} else {
header('Location: index.php');
die();
}
?>
php html login
add a comment |
I've got a php login system on my website that I'm developing offline using XAMPP. Every time I try to use the login system I keep getting this error:
localhost redirected you too many times.
I've tried checking around but can't find any help.
Here's my code in index.php:
<?php
session_start();
$userKey = $_POST['userKeySubmitted'];
$keylist = file('priv/keys.txt');
$success = false;
foreach ($keylist as $key) {
$user_details = explode('|', $user);
if ($user_details[0] == $userKey) {
$success = true;
if ($success) {
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
break;
} else {
break;
}
}
}
?>
....
<form method="post" action="index.php">
<input type="text" name="userKeySubmitted" class="form-control" placeholder="XXXX-XXXX-XXXX-XXXX">
<br>
<button type="submit" name="submit" class="btn btn-info btn-fill pull-right">Login</button>
<div class="clearfix"></div>
</form>
In user.php:
<?php
session_start();
if (isset($_SESSION['userKey'])) {
$key = $_SESSION['userKey'];
echo "<h1>Welcome</h1>";
} else {
header('Location: index.php');
die();
}
?>
php html login
1
Where is$userdefined?
– Akintunde-Rotimi
Dec 31 '18 at 13:55
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15
add a comment |
I've got a php login system on my website that I'm developing offline using XAMPP. Every time I try to use the login system I keep getting this error:
localhost redirected you too many times.
I've tried checking around but can't find any help.
Here's my code in index.php:
<?php
session_start();
$userKey = $_POST['userKeySubmitted'];
$keylist = file('priv/keys.txt');
$success = false;
foreach ($keylist as $key) {
$user_details = explode('|', $user);
if ($user_details[0] == $userKey) {
$success = true;
if ($success) {
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
break;
} else {
break;
}
}
}
?>
....
<form method="post" action="index.php">
<input type="text" name="userKeySubmitted" class="form-control" placeholder="XXXX-XXXX-XXXX-XXXX">
<br>
<button type="submit" name="submit" class="btn btn-info btn-fill pull-right">Login</button>
<div class="clearfix"></div>
</form>
In user.php:
<?php
session_start();
if (isset($_SESSION['userKey'])) {
$key = $_SESSION['userKey'];
echo "<h1>Welcome</h1>";
} else {
header('Location: index.php');
die();
}
?>
php html login
I've got a php login system on my website that I'm developing offline using XAMPP. Every time I try to use the login system I keep getting this error:
localhost redirected you too many times.
I've tried checking around but can't find any help.
Here's my code in index.php:
<?php
session_start();
$userKey = $_POST['userKeySubmitted'];
$keylist = file('priv/keys.txt');
$success = false;
foreach ($keylist as $key) {
$user_details = explode('|', $user);
if ($user_details[0] == $userKey) {
$success = true;
if ($success) {
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
break;
} else {
break;
}
}
}
?>
....
<form method="post" action="index.php">
<input type="text" name="userKeySubmitted" class="form-control" placeholder="XXXX-XXXX-XXXX-XXXX">
<br>
<button type="submit" name="submit" class="btn btn-info btn-fill pull-right">Login</button>
<div class="clearfix"></div>
</form>
In user.php:
<?php
session_start();
if (isset($_SESSION['userKey'])) {
$key = $_SESSION['userKey'];
echo "<h1>Welcome</h1>";
} else {
header('Location: index.php');
die();
}
?>
php html login
php html login
edited Dec 31 '18 at 13:57
Akintunde-Rotimi
3,71731123
3,71731123
asked Dec 31 '18 at 13:50
LunaboosterLunabooster
11
11
1
Where is$userdefined?
– Akintunde-Rotimi
Dec 31 '18 at 13:55
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15
add a comment |
1
Where is$userdefined?
– Akintunde-Rotimi
Dec 31 '18 at 13:55
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15
1
1
Where is
$user defined?– Akintunde-Rotimi
Dec 31 '18 at 13:55
Where is
$user defined?– Akintunde-Rotimi
Dec 31 '18 at 13:55
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15
add a comment |
1 Answer
1
active
oldest
votes
You're redirecting before you set the session value:
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
Then you check for the value and, if it's not there, redirect back:
if (isset($_SESSION['userKey'])){
//...
}
else {
header('Location: index.php');
die();
}
Since it wasn't set, this redirects back. This continues in an infinite loop between the two pages.
Set the value before redirecting:
$_SESSION['userKey'] = $userKey;
header('Location: user.php');
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988220%2flogin-system-using-txt-file-throws-error-localhost-redirected-you-too-many-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're redirecting before you set the session value:
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
Then you check for the value and, if it's not there, redirect back:
if (isset($_SESSION['userKey'])){
//...
}
else {
header('Location: index.php');
die();
}
Since it wasn't set, this redirects back. This continues in an infinite loop between the two pages.
Set the value before redirecting:
$_SESSION['userKey'] = $userKey;
header('Location: user.php');
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
add a comment |
You're redirecting before you set the session value:
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
Then you check for the value and, if it's not there, redirect back:
if (isset($_SESSION['userKey'])){
//...
}
else {
header('Location: index.php');
die();
}
Since it wasn't set, this redirects back. This continues in an infinite loop between the two pages.
Set the value before redirecting:
$_SESSION['userKey'] = $userKey;
header('Location: user.php');
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
add a comment |
You're redirecting before you set the session value:
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
Then you check for the value and, if it's not there, redirect back:
if (isset($_SESSION['userKey'])){
//...
}
else {
header('Location: index.php');
die();
}
Since it wasn't set, this redirects back. This continues in an infinite loop between the two pages.
Set the value before redirecting:
$_SESSION['userKey'] = $userKey;
header('Location: user.php');
You're redirecting before you set the session value:
header('Location: user.php');
$_SESSION['userKey'] = $userKey;
Then you check for the value and, if it's not there, redirect back:
if (isset($_SESSION['userKey'])){
//...
}
else {
header('Location: index.php');
die();
}
Since it wasn't set, this redirects back. This continues in an infinite loop between the two pages.
Set the value before redirecting:
$_SESSION['userKey'] = $userKey;
header('Location: user.php');
answered Dec 31 '18 at 13:54
DavidDavid
148k28143209
148k28143209
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
add a comment |
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
Thanks. This worked!
– Lunabooster
Dec 31 '18 at 14:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988220%2flogin-system-using-txt-file-throws-error-localhost-redirected-you-too-many-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Where is
$userdefined?– Akintunde-Rotimi
Dec 31 '18 at 13:55
I changed It. It was a mistake, it was supposed to be called $key
– Lunabooster
Dec 31 '18 at 14:15