I have a login page, and i have a problem in my page because when the user is already login and she accidentally press the back button and when she try to login she can login..i want that when she already login she could not login again..
I use sessions to do this.
BTW on company.php do you check whether the user is logged in? is there anything stopping any randomer from just going to the url/company.php?
I use sessions to do this.
BTW on company.php do you check whether the user is logged in? is there anything stopping any randomer from just going to the url/company.php?
i have no code to check if the user is logged in and also theres no code for stopping any randomer from going to company.php
Alright well this login script has a few holes in it lol.
So lets start off, if you have never used sessions before they are more or less variables that can be used on all pages. Similar to cookies but the information is not stored on the client.
At the start of all the scripts you want to use sessions you must write this:
PHP Code:
session_start(); //starts the session lol
session_regenerate_id(); //for security purposes.
I try the code that you suggested i put on hte top og my index page or login page
Here is the code
PHP Code:
<?php
session_start();
session_regenerate_id();
$_SESSION['loggedin'] = true;
if(!$_SESSION['loggedin']){
//whatever you want to do with this randomer, redirect or something.
header('Location:index.php');
}
//require_once 'conn.php';
$db_name="dspi";
mysql_connect("localhost", "root", "") or die("Cannot connect to server");
mysql_select_db("$db_name")or die("Cannot select DB");
$sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error());
$ct = mysql_num_rows($sql);
$sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error());
$ct = mysql_num_rows($sql);
if($ct == 1) {
// im guessing this means that the user is valid.
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value.
$row = mysql_fetch_assoc($sql);
$sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error());
$ct = mysql_num_rows($sql);
if($ct == 1) {
// im guessing this means that the user is valid.
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value.
$row = mysql_fetch_assoc($sql);
if(!$_SESSION['loggedin']){
//the user is not logged in, move them away from here
header('Location:index.php');
}
Hopefully I have been able to help.
Thank you for the codes and trying to helped me. i try the code you suggested, and the output is I cant view the index pae and when i try to view the company page the url appear is the url of index. I have no idea why it happened
$sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error());
$ct = mysql_num_rows($sql);
if($ct == 1) {
// im guessing this means that the user is valid.
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value.
$row = mysql_fetch_assoc($sql);
$sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error());
$ct = mysql_num_rows($sql);
if($ct == 1) {
// im guessing this means that the user is valid.
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value.
$row = mysql_fetch_assoc($sql);
Yes its needed or required...but my else condition
PHP Code:
else{
Header ('Location:index.php');
echo 'Wrong Username and Department';
did not work the echo.
and one thing i am concern is that is it possible that i put in session to store the username and department?and where i can put it? and also in all my other webpages i put session to know that user is login??
Thank you for helping me..and sorry if i have a lot of question regarding my problem because i am new in creating website and using php..I hope you understand me...
yeah im not too sure about all the if else statements lol.
and one thing i am concern is that is it possible that i put in session to store the username and department?and where i can put it? and also in all my other webpages i put session to know that user is login??
Yeah you can pretty much put anything in a session.
you can put the username and department in sessions
PHP Code:
$_SESSION['username'] = $row['username'];
As for the department
PHP Code:
$_SESSION['department'] = $row['department'];
you would put this code straight after:
PHP Code:
if($ct == 1) { // im guessing this means that the user is valid. $_SESSION['loggedin'] = true; // now that the user is valid we change the session value. $row = mysql_fetch_assoc($sql);
and for the last question, yeah you can just put the same code on all of the other pages to check if the user is logged in our out.
Not Logged In
PHP Code:
if(!$_SESSION['loggedin']){ //the user is not logged in, move them away from here header('Location:index.php'); }
you can put the username and department in sessions
PHP Code:
$_SESSION['username'] = $row['username'];
As for the department
PHP Code:
$_SESSION['department'] = $row['department'];
you would put this code straight after:
PHP Code:
if($ct == 1) {
// im guessing this means that the user is valid.
$_SESSION['loggedin'] = true; // now that the user is valid we change the session value.
$row = mysql_fetch_assoc($sql);
and for the last question, yeah you can just put the same code on all of the other pages to check if the user is logged in our out.
Not Logged In
PHP Code:
if(!$_SESSION['loggedin']){
//the user is not logged in, move them away from here
header('Location:index.php');
}
logged in
PHP Code:
if($_SESSION['loggedin']){
//user is logged in
}
BTW have you made a log out script?
I dont have log out yet..i try the code and i update you whta would be result..Thank you
Bookmarks