Username and password not saving [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i'm a PHP begginer. I'm actually trying to make a simple login system. I did it, but only for one page... Like you login then you go to index, it works... If i want to go to another page it requires me again to login. I've tried to make the form like this:
<form action="index.php" action="stats.php" method="post">
The both classes have the method POST. I've tried to double the form too like:
<form action="index.php" method="post">
<form action="stats.php" method="post">
It still doesn't work.

First of all you need to have
session_start();
on every page you need to check if the user is logged in
Then at the page that you decide if the credentials are correct you need to set a session variable eg.
$_SESSION['UserIsLoggedIn'] = true;
$_SESSION['UserId'] = $userid;
It is good to create a function that will tell you if the user is logged in
function UserIsLoggedIn(){
if (isset($_SESSION['UserIsLoggedIn']) && $_SESSION['UserIsLoggedIn'])
return true;
else
return false;
}
So every time you need to check if the user is logged in you should check the following
if (UserIsLoggedIn()) { ... }

Related

Php redirect, not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a form with an add user button. If the user is added, the page should redirect to another page which would allow you to edit that user.
What I currently do is:
if ($user_added) {
<META HTTP-EQUIV=refresh CONTENT='0;
URL=<php echo $root. "Path1/path2/path3/userEdit.php?ID=".$newUser->GetID(); ?>'>
NOTE: ID is not something you can switch to easily, its a different more complicated number that i generate randomly whenever I add users, for the sake of the question i kept it simple.
UPDATE: This changes the page to the userEdit page but it does not load the data from the new user.
You are mixing html and php. PHP redirects can be done with a header BEFORE the page loads like so:
<?php header('Location: http://mywebsite.com/index.php'); ?>
But again this only works if the content has not yet been sent.
Also:
URL=<php echo $root. "Path1/path2/path3/userEdit.php?ID=".$newUser->GetID(); ?>'>
This is also insecure because any user can access any user's page simply by changing the url.

How can I prevent someone that didn't login into a page? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I prevent someone that didn't login into a page (control panel) and posting something?
I am using PHP and mysql for the login.
I assume you mean from posting something rather than and posting something. The simple explanation is that on any page that requires authorization, you need to check whether the current user is authorized. The simplest way to do this with a secure cookie and is built into PHP via the Session module.
On the login page you would have something like:
if (log_user_in($username, $password)) {
$_SESSION['authorized'] = true;
}
On pages requiring authorization, you would have a check like:
session_start();
if (!isset($_SESSION['authorized']) || !$_SESSION['authorized']) {
// redirect user away
}
Mechanisms for handling the actual logging in and modularizing the session handling/gatekeeping have been done to death and are baked into most CMS or frameworks. If you are using a CMS or framework, you should look into the specifics of authorization/authentication for it which will probably abstract a lot of what I've told you.

Starting a PHP Session from HTML log in form [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
what I want to do is start a php session for the username, right after the form so it brings up the username once the user has pressed submit.
Here's the code for the form I have:
<form name="login" method="post">
<input type="text" name="username" placeholder="Username"/>
<input type="submit" name="login" value="Login" />
</form>
What do I do after that? Thanks.
Here is a simplified way; put this on top of the page (above) the html code:
if (isset($_POST['login']) {
session_start();
$_SESSION['name'] = $_POST['name'];
}
Now use that new Session variable where you need as the username. If you close your browser. It's no longer stored. (Unless you set a cookie.)

How to check if user is logged in at custom page in Opencart [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a custom page in Opencart. In this page I am using PHP and Javascript based text editor. Now I need to check if user is logged in so that some more options are displayed. I had searched on Google but I did't find anything.
In Your controller (of that custom page) You can do this:
if($this->customer->isLogged()) {
echo "Customer is logged in and his ID is " . $this->customer->isLogged();
} else {
echo "Customer is not logged in";
}
This assumes that the custom page is within frontend and by user You actually mean customer (this assumption was done just because in backend every user has to be logged in prior to display any page).

How to use session id from one page to another page? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In home page (http://mysite.com/) Running in one separate page.
when click the login button from(http://mysite.com/) this page, it will be redirect to another page.
I want to do the following below:
Before login, on the home page the chat function will be offline..
After login the other page the chat function will be online on home page.
How do I link these two pages?
Make sure to use session_start(); at top of pages.
And make sure that when they log in, you set $_SESSION['loggedin'] == TRUE; and then unset the session var on log out.
if ($_SESSION['loggedin'] == TRUE){
//include chatbox code or whatever it is
}else{
//don't add an else if you don't want anything if not logged in, but if you do, code away here
}

Categories