Need help in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marse
Forum Newbie
Posts: 1
Joined: Mon May 18, 2015 12:58 am

Need help in PHP

Post by marse »

I am currently working on a project where I have two pages: a login page and a home page. The home page is after the login page, whenever the user inputs his username and password correctly in the login page he will be directed to the home page. Here is the problem, if i input the url of the home page in the browser, it will open even if i did not go through the login page. How do i put restrictions in the home page? How do i put a message saying "you are restricted to access this page, please go back to login page" and then it redirects the user to the login page. If you know some website with tutorials or vidoes of tutorials please include them, it will be a great help. Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help in PHP

Post by requinix »

What you're looking for is called "sessions" - search around, you'll find plenty of information.

The login page stores a value in the session indicating the person logged in. Typically you'll also store the username and other bits of information that may be useful. Except the password. Don't store the password in there.
The homepage checks for that value in the session, and if it's not there then it can redirect. However for a homepage I'd suggest you keep the user there (no redirect) and show a different version to people who haven't logged in. It's a good practice: the homepage is where people get to your website the most, and so you don't want to send them somewhere else immediately.
On that note, I wouldn't put this privileged information on the homepage. That's a place where you should have an overview of the site. The information can go on another page - that way you can redirect without worry since it isn't that people accidentally get to it while logged out.
vincebiggz
Forum Newbie
Posts: 3
Joined: Mon May 18, 2015 1:52 am

Re: Need help in PHP

Post by vincebiggz »

As you've heard from above, you need to have a secure session code in your protected pages. You can view this tutorial http://webcodingtutorial.blogspot.com/2 ... -with.html
Post Reply