Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Login with file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Login with file

Postby viktorjano » Tue Apr 16, 2013 10:10 pm

Hi!
I am making a web portal with simple log in system, that uses text-file based way of checking the usernames and passwords. But, I can not solve a problem that emerged. The panel for logging in is on my index page, so when user logs in, a message appears that confirms whether he managed logging in or not on the same page.
After logging in, a log out button appears. But, when user goes to some other page of the portal, let's say contact page, and then he decides to get back to the index page, the log in panel appears again and I expect that session runs and the log out button to appear..
Here is a excerpt of the code on the top of my index page:

Code: Select all
<?php if(isset($_SESSION['status']))
{
session_start();
}
//then here goes the code that checks if this is the time when user logs in
if(array_key_exists('login', $_POST))
{
//check for username and password
//if (correct)
{
$_SESSION['status'] = 'logged'
session_start();
}
}
?>

//Then in the body of the page I have if statements, if user is logged show log out button, if is not show the log in panel.


Then, on my other pages I also check at the top of the page

Code: Select all
<?php if(isset($_SESSION['status']))
{
session_start();
}
?>


So, once again the problem emerges when I leave the index page and then I get back once again, I would expect the log out button to show. In other words I presume that the variable $_SESSION['logged'] is remembered in the session cookie.

Thanks!
viktorjano
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Apr 16, 2013 9:21 pm

Re: Login with file

Postby johnj » Wed Apr 17, 2013 5:22 am

If you are using session_start() then it has to be the first line in the php file.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Login with file

Postby seandisanti » Wed Apr 17, 2013 2:10 pm

Here you go, in this video from jream, he explains creating and using a session wrapper to selective start a session or get an existing instance of it as necessary.

http://jream.com/learning/videos/php-oo ... on-wrapper
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Login with file

Postby viktorjano » Fri Apr 19, 2013 11:28 am

Thanx guys,

but I must say that that does not solve may problem.
For JohnJ, my session_start(); method is actually the first line, since as far as I know that refers, to not have any output before you call this method. And I don't.
For Sean, that is great tutorial but not solving my problem.

Just for a try, I echo the $_SESSION['status'] in the body of the index page, with the help of the isset() function, and it displays only when I log on the page, or I reload the page, but when I go to other page of the same site (I mean same folder), as I wrote earlier, for example, contact page, it looses somehow. So how do I keep this variable set. It has to be something with the scope of the session. Hmmm, it already drives me crazy, what should I do?

Thanx anyway!
viktorjano
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Apr 16, 2013 9:21 pm

Re: Login with file

Postby seandisanti » Fri Apr 19, 2013 12:10 pm

You must session_start() on every page using session variables. here's another great tutorial targeted more on the session than the wrapper.
http://jream.com/learning/videos/php-ba ... e-security
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.