Hello I have a weird Problem with PHP and HTML. I made a Page with a contact form that contains a Buttons that redirects to a workings space that is only accessable when you are loged in. Else you are redirected to the login page. First my back button in under contact is defined like this:
<a href="https://myhompage.com/admin/index.php" data-role="button" data-icon="arrow-l" style="width: 60px;">Back</a>
Then in the index.php is a file called with require_once that checks if the session is valid or not. If valid do nothing if not reirect to login.php
header("Location: https://lmyhomepage.com/login.php", true, 302);
exit;
I Tryed just ../login.php with and without the 2 variables. I Change code 302 to 303. I changed exit to die and nothing works right.
On Top of my index.php file is short entry:
<?php
require_once("check_session.php");
?>
Below that is only HTML and javascript The check_session.php looks like tihs:
<?php
// Start or resume session
$a = session_id();
if ($a == '') session_start();
$key = "";
// Check if session ID is not present or valid
if (isset($_SESSION['sessionid']) && isset($_SESSION["key"])) {
if ($_SESSION['sessionid'] != session_id()) {
// Redirect to login page
header("Location: https://myhomepage.com/login.php");
exit;
} else {
require_once("includes/create-connection.php");
$sqlAbfrage = "select id from rechte where schluessel = ?;";
if ($sqlAbfrage = mysqli_prepare($verbindung, $sqlAbfrage)) {
// UserID als Parameter für Abfrage übergeben ("i" bedeutet Integer)
mysqli_stmt_bind_param($sqlAbfrage, "s", $_SESSION["key"]);
// Abfrage ausführen
mysqli_stmt_execute($sqlAbfrage);
// Gespeicherten Startcode in Variable $ergebnisStartcode ablegen
mysqli_stmt_bind_result($sqlAbfrage, $key);
// Wert zuweisen
mysqli_stmt_fetch($sqlAbfrage);
// Abfrage schließen
mysqli_stmt_close($sqlAbfrage);
}
if ($key == 2) {
header("Location: ../user/index.php", true, 303);
exit;
} else if ($key != 1) {
header("Location: https://myhomepage.com/login.php");
exit;
}
}
} else {
// Redirect to login page
header("Location: https://myhomepage.com/login.php");
exit;
}
?>
The URL in my browser says https://myhompage.com/admin/index.php while login.php is mostly loaded exept for js code and an image. The JS code is directly in the login.php implemented but does not work. If I press F5 or just normaly reload the page the URL Changes to https://myhompage.com/login.php like it is supposed to and then everything works fine. Through more testing I realized that my Homepage still believes it is in the /admin area and tries to load the image and the JS/CSS files from that folder into the login.php which in a folder on top. Project directory: HTML -> web (here is my login.php with css and js files) -> admin (hier is the index.php and the check_session.php and additional js and css files) Does anyone now where I go wrong?
Answers
It seems like your issue stems from how you handle redirects and URLs in your application. Let's break down the potential problems and suggest solutions:
-
Redirect Loop or Incorrect Redirects:
- Ensure that your redirection logic is correct and doesn't create a loop. Check if there are any conditions that could lead to multiple redirects.
- Verify that the URLs in your
header("Location: ...")
calls are correct and point to the intended pages. Make sure you're using absolute URLs when redirecting across different directories.
-
Session Handling:
- Double-check your session management code (
session_start()
, checking session IDs, etc.) to ensure that sessions are started and maintained correctly across your pages. - Confirm that session variables are set and accessed accurately throughout your application.
- Double-check your session management code (
-
Relative URLs and File Paths:
- When linking to resources like images, JavaScript, or CSS files, ensure that you're using correct relative paths or absolute URLs. In your case, if
login.php
is in a different directory fromindex.php
, make sure the paths to your resources are adjusted accordingly. - For example, if your
login.php
file is located in a directory above your CSS and JavaScript files, you might need to adjust the paths in your HTML to navigate up one directory before accessing those resources.
- When linking to resources like images, JavaScript, or CSS files, ensure that you're using correct relative paths or absolute URLs. In your case, if
-
Headers and Output Buffering:
- Ensure that no output is sent to the browser before calling
header()
for redirection. Output sent before headers will cause PHP to throw an error. - You might want to check for any whitespace or unintended characters before the
<?php
opening tag in your files, as these can cause output to be sent prematurely.
- Ensure that no output is sent to the browser before calling
-
Testing Environment:
- Test your application thoroughly in different environments (local, staging, production) to ensure consistent behavior.
- Use browser developer tools to inspect network requests, redirects, and errors. This can help identify any incorrect URLs or missing resources.
By carefully reviewing and debugging your code for these potential issues, you should be able to resolve the redirection problem and ensure that your application functions correctly.