Hey guys! I want to make a simple login system with PHP, I am pretty noob with php, but have done C++ for like year so understanding the code shouldn't be a problem. Things are done a bit different in the web than in simple program so.. I found this http://www.phpeasystep.com/phptu/6.html and the thing I want to do is when you come to page it asks your password etc and then when you login the page changes to your control panel and you must be logged in to access it. Any ideas guys, and the login is for learning!

Hey guys! I want to make a simple login system with PHP, I am pretty noob with php, but have done C++ for like year so understanding the code shouldn't be a problem. Things are done a bit different in the web than in simple program so.. I found this http://www.phpeasystep.com/phptu/6.html and the thing I want to do is when you come to page it asks your password etc and then when you login the page changes to your control panel and you must be logged in to access it. Any ideas guys, and the login is for learning!

That tutorial should do it for you. When you get to login_success.php, add an else here:

if(!session_is_registered(myusername)){
header("location:main_login.php");
}

Then use the $_SESSION to determine the user who is logged in and display their control panel.

So I should do like if I want to test is user logged in I put statement and if user is logged in I include php file with the control panel or if he isn't I include php file with the login form, that way?

<?
session_start();
if(!session_is_registered(myusername))
{
	header("location:Main.php");
	echo "Something";
}
else
{
	echo "lol";
}
?>

<html>
<body>
Login Successful
</body>
</html>

This part doesn't somehow work, it won't echo those things :S

The checklogin.php page in that tutorial already validates the login. If you find that the $_SESSION variable is set (it's set in the checklogin.php page), you already know that they are logged in successfully and you can present the control panel or whatever other content you need based on the username.

So I add the control panel code to login_success file? And how I can put my pages URL look like www.page.com?page=Main and Page=ControlPanel and so on? And what if user goes straight to login_success.php?

Bah I can't get it, why can't I even echo anything in that file they just won't show up!

So I add the control panel code to login_success file?

Yes. Or create a new page that you can link to if you like. After setting the session variables you can put the

session_start()

function on any page and access any session variables you have set.

And how I can put my pages URL look like www.page.com?page=Main and Page=ControlPanel and so on?

That's kind of a whole other topic. You may want to start a new thread for that, but basically your index page will be getting the $_PUT variables and deciding what content to show based on that.

And what if user goes straight to login_success.php?

That's what the first part of login_success.php is for:

if(!session_is_registered(myusername)){
header("location:main_login.php");
}

It says that if myusername is not registered as a session variable, re-direct back to the login page (cause you didn't log in yet, you cheater!).

Here's a good overview of the basics of sessions: http://www.tizag.com/phpT/phpsessions.php

Well if I try go straight there it doesn't throw me out somehow :S

I just don't get it why it doesn't redirect if I enter it straight on the browser I mean the login_success.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.