gudfry Posted July 21, 2008 Share Posted July 21, 2008 hi to all; I have a PHP that keep the username and password for the user required to log-in the index page. but i have problrm problem with it. here is a pice of code. <?php $user = 'xxxxxxxx'; $pass = 'xxxxxxxx'; if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) { echo '<a href="page goes here">Clik here to view the site</a>'; } else { echo 'Wrong username and password.<br/>'; echo '<a href="index.html?menu=all">Go-back</a>'; } ?> how woul i do this using session to keep the password and the username hidden. and also if the user is need to log-on the page before enter the index page. thank for the advance. Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/ Share on other sites More sharing options...
LooieENG Posted July 21, 2008 Share Posted July 21, 2008 <?php session_start(); $user = 'xxxxxxxx'; $pass = 'xxxxxxxx'; if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) { $_SESSION['loggedIn'] = 1; echo '<a href="page goes here">Clik here to view the site[/url]'; } else { echo 'Wrong username and password. '; echo '<a href="index.html?menu=all">Go-back[/url]'; } ?> And on the next page <?php session_start(); if ($_SESSION['loggedIn'] == 1) { // user is logged in } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595162 Share on other sites More sharing options...
j007ha Posted July 21, 2008 Share Posted July 21, 2008 <?php session_start(); $user = 'xxxxxxxx'; $pass = 'xxxxxxxx'; if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) { $_SESSION['islogin']=true; echo '<a href="page goes here">Clik here to view the site[/url]'; } else { echo 'Wrong username and password. '; echo '<a href="index.html?menu=all">Go-back[/url]'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595163 Share on other sites More sharing options...
gudfry Posted July 21, 2008 Author Share Posted July 21, 2008 thanks everyone. I do everything what evryone told me. but i have still problem with the session, because I still view my index page, and it doesnt require me to to log in. sory for my bad question I have this page to confirm password and the user name. session_start(); $user = 'xxxxxxx'; $pass = 'xxxxxxx'; if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) { $_SESSION['loggedIn'] = 1; echo '<a href="main_index.php">Clik here to view the site'; } else { echo 'Wrong username and password.'; echo '<a href="index.html?menu=all">Go-back'; } this is my another page which is my index <?php session_start(); if ($_SESSION['loggedIn'] == 1) { // user is logged in } ?> <?php switch ($menu) { case 'hotel': { $includeFile='bodyhotel.php'; break; } default: { $includeFile='bodycity.php'; break; } } require_once($includeFile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595173 Share on other sites More sharing options...
LooieENG Posted July 21, 2008 Share Posted July 21, 2008 For each page that should be hidden to users that aren't logged in, right at the top of the page, so this <?php if ($_SESSION['loggedIn'] != 1) { // User isn't logged in exit('Log in!'); } // Rest of code ?> Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595176 Share on other sites More sharing options...
matthewhaworth Posted July 21, 2008 Share Posted July 21, 2008 My post contained information that was incorrect. Please ignore it. Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595188 Share on other sites More sharing options...
gudfry Posted July 21, 2008 Author Share Posted July 21, 2008 thanks i got it now. i solve it Quote Link to comment https://forums.phpfreaks.com/topic/115769-solved-how-to-create-a-session-log-in/#findComment-595193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.