Jump to content

need help with login php/html

shadowss
Go to solution Solved by Ominous,

I assume it's "opret_opslag.php" that you want to restrict access to? You need to check if a user is logged in.

 

Include this in the header:

<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION['brugernavn'])) {
    // Redirect to login page
    header("Location: login-side.php");
    exit();
}
?>

Ideally, you should also perform the opposite of this action on login-side.php. Send logged-in users to opret_opslag.php instead of letting them view the login page.

hello i am a student that doing an assignment where im trying to make a login system only using php and html im able to login and get back to the loginside with a logout button but i need to not be able to access the side unless i login but not sure how to not sure if it is cause its not destroying the session or not at all starting the session or something but for some reason its not working hope you all can help me here is the code i have so far 


image.thumb.png.8b34d8718efed2832c57d17704569150.pngimage.thumb.png.331ce92853316c8b938a0d23cb1891fa.pngimage.thumb.png.0f4b1b9e052540cb0dd8d4921937546c.png

Link to comment
Share on other sites

Link to post
Share on other sites

I assume it's "opret_opslag.php" that you want to restrict access to? You need to check if a user is logged in.

 

Include this in the header:

<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION['brugernavn'])) {
    // Redirect to login page
    header("Location: login-side.php");
    exit();
}
?>

Ideally, you should also perform the opposite of this action on login-side.php. Send logged-in users to opret_opslag.php instead of letting them view the login page.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Ominous said:

I assume it's "opret_opslag.php" that you want to restrict access to? You need to check if a user is logged in.

 

Include this in the header:

<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION['brugernavn'])) {
    // Redirect to login page
    header("Location: login-side.php");
    exit();
}
?>

Ideally, you should also perform the opposite of this action on login-side.php. Send logged-in users to opret_opslag.php instead of letting them view the login page.

yea thats right thanks ill try that 

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Ominous said:

I assume it's "opret_opslag.php" that you want to restrict access to? You need to check if a user is logged in.

 

Include this in the header:

<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION['brugernavn'])) {
    // Redirect to login page
    header("Location: login-side.php");
    exit();
}
?>

Ideally, you should also perform the opposite of this action on login-side.php. Send logged-in users to opret_opslag.php instead of letting them view the login page.

found out i should have the if statement like this 

if (isset($_SESSION['brugernavn'])) {
    // Redirect to login page
}

instead of like this 

if (!isset($_SESSION['brugernavn'])) {
    // Redirect to login page
    header("Location: login-side.php");
    exit();
}

but you helped a lot for me to figure it out so thx for the help 
Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, shadowss said:

found out i should have the if statement like this 

if (isset($_SESSION['brugernavn'])) {
    // Redirect to login page
}

That doesn't seem right. isset() returns true if a variable is set. If $_SESSION['brugernavn'] is set, a user is logged in. Unless I've misunderstood something, I'd double-check the logic in your code.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Ominous said:

That doesn't seem right. isset() returns true if a variable is set. If $_SESSION['brugernavn'] is set, a user is logged in. Unless I've misunderstood something, I'd double-check the logic in your code.

yea i need to see if the user is logged in then they can see the page otherwise they cant 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, shadowss said:

yea i need to see if the user is logged in then they can see the page otherwise they cant 

I understand, but my code should prevent anyone from accessing the page if they are not signed in. It shouldn't have required you to change anything.

 

If you try to access the opret_opslag.php page without being logged in, you should be able to access the page with your changes. I don't believe that is the desired behaviour.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Ominous said:

I understand, but my code should prevent anyone from accessing the page if they are not signed in. It shouldn't have required you to change anything.

 

If you try to access the opret_opslag.php page without being logged in, you should be able to access the page with your changes. I don't believe that is the desired behaviour.

 

no in your code i cant access it whether i am logged in or not with the thing i changed then i can access the webpage if im logged in so if there is a saved session and if there is not a saved session/ im not logged in then it does not show the page 

and 

 

    header("Location: login-side.php");
    exit(); 
this piece of code is just throwing me back to the login page if i change the isset from !isset to isset which is what i would exspect it to do 
Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, shadowss said:

no in your code i cant access it whether i am logged in or not with the thing i changed then i can access the webpage if im logged in so if there is a saved session and if there is not a saved session/ im not logged in then it does not show the page 

and 

 

    header("Location: login-side.php");
    exit(); 
this piece of code is just throwing me back to the login page if i change the isset from !isset to isset which is what i would exspect it to do 

Something is going wrong there.

 

If isset($_SESSION["brugernavn"]) returns true, the user is logged in. That means you redirect to a login page when they are signed in, which is not your desired behaviour. Your session wasn't destroyed. Unless you have changed your code more than you have shown me?

 

Open your program in a private browser and try to access the page. You should be able to access opret_opslang.php without logging in.

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Ominous said:

Something is going wrong there.

 

If isset($_SESSION["brugernavn"]) returns true, the user is logged in. That means you redirect to a login page when they are signed in, which is not your desired behaviour. Your session wasn't destroyed. Unless you have changed your code more than you have shown me?

 

Open your program in a private browser and try to access the page. You should be able to access opret_opslang.php without logging in.

nope cant access with either code without being logged in and your code i cant access it in general with the code i send you i changed it to which is 
if (isset($_SESSION['brugernavn'])) {
    // Redirect to login page
}
then i can access opret_opslag.php when im logged in which is the desired behaviour and not when im not logged in 

Link to comment
Share on other sites

Link to post
Share on other sites

Do you understand why that doesn't make sense?

 

This code

27 minutes ago, shadowss said:

if (isset($_SESSION['brugernavn'])) {
    // Redirect to login page
}

Effectively means:

IF user_is_logged_in THEN
	send_to_login_page
ENDIF

The code works, but it is not the correct behaviour. Something is wrong. Can you share your code again so I can see what's going on?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Ominous said:

Do you understand why that doesn't make sense?

 

This code

Effectively means:

IF user_is_logged_in THEN
	send_to_login_page
ENDIF

The code works, but it is not the correct behaviour. Something is wrong. Can you share your code again so I can see what's going on?

its not send to login_page it was send to opret_opslag sorry send the wrong thing inbetween have been coding all day today and yesterday 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, shadowss said:

its not send to login_page it was send to opret_opslag sorry send the wrong thing inbetween have been coding all day today and yesterday 

Yep that makes more sense 🙂

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Ominous said:

Yep that makes more sense 🙂

yea didn't see it was send to login page cause been looking at code for too long thats my bad 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×