Hello I have created a login page which is
<form method="POST" action="log.php">
<table border="1" cellpadding="4" cellspacing="0"
style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
width="100%">
<thead>
<tr>
<td style="border: 0px; width: 20%; background-color: transparent;">
Username:</td>
<td style="border: 0px; width: 30%; background-color: transparent;">
<input type="text" name="email" id="email" class="form-control"
autocomplete="off" required="">
</td>
</tr>
</thead>
</table>
<table border="1" cellpadding="4" cellspacing="0"
style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
width="100%">
<thead>
<tr>
<td style="border: 0px; width: 20%; background-color: transparent;">
Password:</td>
<td style="border: 0px; width: 30%; background-color: transparent;">
<input type="password" name="pass" id="pass" class="form-control"
autocomplete="off" required>
</td>
</tr>
</thead>
</table>
</div>
<div class="spacer-20"></div>
</div>
<!-- Social Signup -->
<div class="social-signup">
<span class="or-break"></span>
<center>
<button type="submit" name="login" class="btn btn-primary">
<span class="glyphicon glyphicon-log-in"></span> Login
</button>
No account? Sign up
</form>
and the login Processor is as follows
<?php
// Login Controller
// require('config/config.php');
include './config/config.php';
$userName = $passWord = $name = "";
if (isset ( $_POST ['login'] )) {
$userName = mysqli_real_escape_string ( $con, $_POST ['email'] );
$password = mysqli_real_escape_string ( $con, $_POST ['pass'] );
$pass = md5 ( $password );
// Block handles Doctor Login using the email
$get_doc = "SELECT * FROM doctor_registration WHERE email='$userName' AND password='$pass'";
$run_doc = mysqli_query ( $con, $get_doc );
$rows = mysqli_fetch_array ( $run_doc );
$docId = $rows ['doc_id'];
$docEmail = $rows ['email'];
$name = $rows ['first_name'] . " " . $rows ['last_name'];
if ($userName == $docEmail) {
$_SESSION ['sessionName'] = $name;
$_SESSION ['sessionId'] = $docId;
echo "<script>window.open('doctor_dashboard.php','_self')</script>";
} else {
// Block handles user Login using the username
$get_user = "SELECT * FROM users WHERE username='$userName' AND password='$pass'";
$run_user = mysqli_query ( $con, $get_user );
$rows = mysqli_fetch_array ( $run_user );
$userId = $rows ['user_id'];
$uname = $rows ['username'];
if ($userName == $uname) {
$_SESSION ['sessionName'] = $uname;
$_SESSION ['sessionId'] = $userId;
echo "<script>window.open('user_dashboard.php','_self')</script>";
} else {
echo "<script>alert('Passowrd or username is not correct!')</script>";
}
}
}
?>
and for the dashboard the processor checks the email or username and redirects to one of the dashboard.
i have two dashboards which have the code bellow for starting and storing a session
<?php
session_start ();
include './config/config.php';
include './updateFunction.php';
include './InsertFunction.php';
if (isset ( $_SESSION ['sessionName'] ) && ($_SESSION ['sessionId'])) {
header ( "location:index.php" );
}
?>
and each time i try to login in both dashboards i get the error:
Undefined index: sessionName
You should use session_start() before using $_SESSION['sessionName'] = $name
in the login processor.
So preferably you should use session_start() at the beginning of the page
Put session_start(); in your config.php after <?php
then call config.php on every page on after <?php
you are getting session error because you haven't use session_start(); in your file
Related
i am trying to redirect my login page to user-home page. my code is not showing any error but login page is not redirecting to user-home page. badly expecting a proper solution. Thanks in advance. the following image is the databse details of signup page.
enter image description here
here is my code of index page
enter code here
<tr>
<td width="200px" height="70px" style="color: gray;"><b>ENTER USERNAME</b></td>
<td width="100px" height="70px" style="border-bottom: 1px solid black;color: gray;" ><input type="text" name="username" placeholder="ENTER USERNAME" style="width: 150px;height: 35px;background:none; border: none;"></td>
</tr>
<tr>
<td width="200px" height="70px" style="color: gray;"><b>ENTER PASSWORD</b></td>
<td width="200px" height="70px" style="border-bottom: 1px solid black;color: gray;" ><input type="PASSWORD" name="password" placeholder="ENTER PASSWORD"style="width: 150px;height: 35px;background: none; border: none;"></td>
</tr>
<tr>
<td><input type="submit" name="login" value="login"style="width: 150px;height: 35px; background:none;border-radius: 10px; border-color: gray; padding: 10px "></td>
</tr>
</form>
<h2>dont have an account signup here</h2>
<li>signup</li>
</table>
<?php
if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$query = $db->query("select * from signup where username = '$username' && password = '$password'");
$count = $query->rowcount();
$row = $query->fetch();
if ($count > 1){
session_start();
$_SESSION['id'] = $row['user_id'];
header('location:user-home.php');
}else{
header('location:index.php');
}
}
?>
The problem is with the following condition
if($count > 1){
}
If there's an entry with the associated username and password in the database then $query->rowcount(); will return the no of rows selected which should be one(1).
The condition should be
if($count == 1){
//home-page
}
else{
//index page
}
Please change if ($count > 1){} to if ($count > 0){}
Your code looks like :
if ($count > 0){
session_start();
$_SESSION['id'] = $row['user_id'];
header('location:user-home.php');
}else{
header('location:index.php');
}
I am building a login system where there is a member and a user. I have a table that has a column of name, username, password, email and type. Type designates the type of user. I have created already a working log-in form. My problem is every time I try to log in it does not redirect to the designated page. It stays on the same page but gives me a blank page. I've been figuring out what is wrong with the code. Can someone help me ?
Thank you.
PHP Login code:
<?php
include 'try_connect.php';
if (isset($_POST['login'])) {
$user = $_POST['username'];
$pass = $_POST['password'];
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'");
$data = mysql_fetch_array($hsl);
$username = $data['username'];
$password = $data['password'];
$type = $data['type'];
$name = $data['name'];
if ($user==$username && $pass==$password) {
session_start();
$_SESSION['name']=$name;
if ($type =='admin') {
header('Location: try_admin.php');
}
elseif ($type =='user') {
header('Location:try_user.php');
}
}
}
?>
LOG IN FORM
<form action="try_login.php" method="POST">
<table style="margin-left: 30%; margin-bottom: 1%;">
<tr>
<th style="font-family: Arial; line-height: 5px;">Username:</th>
<td><input type="text" name="username" style="margin-left: 10%; width: 120%; margin-bottom: 5%;"></td>
</tr>
<tr>
<th style="font-family: Arial;">Password:</th>
<td><input type="password" name="password" style="margin-left: 10%; width: 120%; margin-bottom: 0%; margin-top: 10%;"></td>
</tr>
</table>
<input type="submit" value="login" name="login" style="background-color: black; color: white; margin-bottom: 5%; margin-top: 0%; margin-left: 59%; border-color: #89cff0; border-style: double solid;">
Try this:
<?php
include 'try_connect.php';
if (isset($_POST['login'])) {
$user = $_POST['username'];
$pass = $_POST['password'];
$hsl = mysql_query("SELECT name, username, password, type FROM users WHERE username='$user' and password='$pass'");
$data = mysql_fetch_array($hsl);
$username = $data['username'];
$password = $data['password'];
$type = $data['type'];
$name = $data['name'];
if ($user==$username && $pass==$password) {
session_start();
$_SESSION['name']=$name;
if ($type =='admin') {
header("Location: try_admin.php");
}
elseif ($type =='user') {
header("Location:try_user.php");
}
}
}
?>
Use exit() after header(). exit() will terminate your script immediately.
if ($type =='admin') {
header('Location: try_admin.php');
exit();
}
elseif ($type =='user') {
header('Location:try_user.php');
exit();
}
Also make sure your header() function is called before any html output even before <html> tag
hi this is my code which updates an attendance system, but I dont know how to insert attendance for first time. Where should I use insert query? it also shows error and not updating the table. Somebody please guide me
<?php
$connection=mysql_connect("localhost","root","") ordie(mysql_error()
$db=mysql_select_db("Project") or die(mysql_error());
$sql = "SELECT Fac_name FROM Faculty ORDER BY Fac_name ASC ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<form name="Attendence" method="post" action="A.php">
<table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px">
<tbody>
<tr>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Faculty Name</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Abesent</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Present</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Total present</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)) {
?>
<tr>
<td class="table1">
<? $id[] = $rows['Fac_name']; ?><? echo $rows['Fac_name'];?>
</td>
<td class="table1">
<input name="date[<? echo $rows['Fac_name']; ?>]" type="text" >
</td>
<td id="present">
<input type="radio" name="Present[<? echo $rows['Fac_name']; ?>]" checked="checked" >Present
</td>
<td id="absent">
<input type="radio" name="Absent[<? echo $rows['Fac_name']; ?>]" value="ABSENT">Absent
</td>
<td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"></td>
</tr>
<?php }?>
<tr>
<td colspan="7" style="vertical-align:middle; text-align: center;">
<br><br>
<input id="Submit" type="submit" name="Submit" value="Insert" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;">
</td>
</tr>
</tbody>
</table>
</form>
<?php
if(isset($_POST['Submit'])) {
foreach($_POST['Present'] as $id => $value) {
$date=$_POST['date'];
$present=$_POST['Present'];
$absent=$_POST['Absent'];
$sql = "INSERT INTO Attendence(Fac_name, date, Present, Absent) VALUES ('".$id."', '$date[$value]', '$present[$value]', '$absent[$value]', '".$value."') ";
$result = mysql_query($sql);
}
}
if($result) {
//header("location:A.php");
} else {
//print_r ($_POST);
echo "Your entry is not completed at this time.............";
}
if(isset($_POST['submitattend'])) {
set_time_limit(0);
$class1 = $_SESSION['bra'];
$q3 = mysql_query("Select Id from `Faculty` order by `Id` ASC"); // get all roll numbers
$count = mysql_num_rows($q3);
$j = 1;
while($q4 = mysql_fetch_array($q3)) {
if(isset($_POST['chk'.$j])) {
$v2 = $q7['finalattend']+1; //total attendance of student
$v3 = $q7['totalattend']+1; //total attendance taken by teacher
mysql_query("UPDATE `Attendence` SET `finalattend`='".$v2."', `totalattend`='".$v3."' where `attenduser`='".$v1."'") or die(mysql_error());
} else {
$v2=$q7['totalattend']+1;
mysql_query("UPDATE `Attendence` SET `totalattend`='".$v2."' where `attenduser`='".$v1."'") or die(mysql_error());
}
$j=$j+1;
}
header("Location: 12.html"); //logout after taking attendance..
}
?>
$date_value = $date[$value];
$present_value = $present[$value];
$absent_value = $absent[$value];
$check_result = mysql_query("selct count(*) from Attendence where Fac_name = '$id' AND date = '$date_value' AND Present = '$present_value' AND Absent = '$absent_value' ");
if($check_result == 0)
{
// insert query
}
else
{
// update query
}
I think your INSERT query line has Syntax error Please correct it . You have only three row names - Fac_name, date, Present, Absent but are inserting 5 values. Besides that quotes are not used correctly
I am getting a problem where my login details wont let me log into my page :(
so my set up page is
<?php
$db = mysql_connect("$host", "$dblogin", "$dbpassword");
mysql_select_db("$dbname");
$res = mysql_query("SELECT * FROM userdb WHERE email='$email'");
$playerinfo = mysql_fetch_array($res);
$date2 = date("H:i");
function error($type)
{
if($type == "field")
{
echo '<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" bgcolor="#000000" text="#FFFFFF">';
echo '<p align=center><font color="red">You have left fields blank. Please relogin</font></center></p>';
}
elseif($type == "password")
{
echo '<p><center><font color="red">Incorrect password. Please relogin</font></center></p>';
}
}
echo '</body>';
?>
my login page is
<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" bgcolor="#000000" text="#FFFFFF">
<p align="center">
<img border="0" src=logo.jpg></img></p>
<p align="center">
<form action=check.php method=post>
<p align="center"><font size=1><b><font face="Tahoma" size="2">Login.</font></b><br><br>
<samp style="font-weight: normal; font-size: 7pt; font-family: tahoma"><font face="Tahoma" size="2"><b>Email -</fomt></b></font></samp><font face="Tahoma" size="2"><b>
<input type="text" name="email" size="20" style="border: 1px solid black">
</b></font></font><font face="Tahoma" size="2">
<br>
<samp style="font-weight: normal; font-size: 7pt; font-family: tahoma">
<b><font face="Tahoma" size="2">Pass -</font></b></samp><b><font size="1" face="Tahoma"><input type="password" name="password" size="20" style="border: 1px solid black"></font></b><br><input type="submit" value="Log In" style="border: 1px solid black">
<br>
</form>
</body>
and my check.php page is
<?
session_start();
include("setup.php");
if(!$email || !$password) {
error("field");
exit();
}
if($password == $info['password']) {
session_register("password");
session_register("email");
include("top.php");
echo "<p><b><center>Welcome</center></b></p>";
echo "<p><u><b>Members Area</b></u></p>";
echo "<p><center>- <b><a href=>Page</a></b> -</center></p>";
echo "<p><center>- <b><a href=logout.php>Log Out</a></b> -</center></p>";
include("bottom.php");
}
else
{
error("password");
}
?>
I always get a error of You have left fields blank. Please relogin from my index page so I cannot get my user to log in
where have I failed?
I think you have not even set your variables :D That is why it is saying "You have left fields blank"
session_start();
include("setup.php");
After this include
$email = $_POST["email"];
$password = $_POST["password"];
Besides this : Use prepared statements to avoid SQL Injection
The problem may be this:
$db = mysql_connect("$host", "$dblogin", "$dbpassword");
I think you meant:
$db = mysql_connect("{$host}", "{$dblogin}", "{$dbpassword}");
You cannot include variables into a string without the {}.
But even in that case, you need to define those varaibles before this line.
$host = "ipaddress";
Also, once you get beyond that, you are treating strings as booleans:
if(!$email || !$password)
Ok I almost got a dropdown pulling from my DB and posting to it as well to work. I got it to pull down the data and for it to submit to the DB. Still a stump. If I have example "ABC Trucking" as an option. It only posts "ABC" to table1. For whatever reason it doenst post two words? Any Ideas? See where the carriername dropdown is in the div.
My Code:
<?php
if (isset($_POST["submit"]) && $_POST["submit"] == "Submit")
{
for ($count = 1; $count <= 9; $count++)
{
$fields[$count] = "";
if (isset($_POST["field" . $count . ""]))
{
$fields[$count] = trim($_POST["field" . $count . ""]);
//echo $fields[$count] . "<br />";
}
}
$con = mysql_connect("local", "user", "pass");
mysql_select_db("DB", $con);
$carriername = mysql_real_escape_string($_POST['carriername']);
$fromzip = mysql_real_escape_string($_POST['fromzip']);
$tozip = mysql_real_escape_string($_POST['tozip']);
$typeofequipment = mysql_real_escape_string($_POST['typeofequipment']);
$weight = mysql_real_escape_string($_POST['weight']);
$length = mysql_real_escape_string($_POST['length']);
$paymentamount = mysql_real_escape_string($_POST['paymentamount']);
$contactperson = mysql_real_escape_string($_POST['contactperson']);
$loadtype = mysql_real_escape_string($_POST['loadtype']);
$insert = "INSERT INTO table1 (`carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype`) VALUES('$carriername' ,'$fromzip' ,'$tozip' ,'$typeofequipment' ,'$weight' ,'$length' ,'$paymentamount' ,'$contactperson' ,'$loadtype');";
mysql_query($insert) or die(mysql_error());
$select = "SELECT `carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` FROM `table1` ORDER BY `paymentamount` DESC;";
$result = mysql_query($select) or die(mysql_error());
}
?>
</script>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<div
style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
<div
style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
<table style="font-weight: normal; width: 100%; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<table
style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<tbody>
<tr>
<td style="width: 10%;">Carrier:</td><td>
<?php
$con = mysql_connect("local", "user", "pass");
mysql_select_db("DB", $con);
$query=("SELECT * FROM table2");
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
echo "<select name=carriername>";
while($row=mysql_fetch_array($result)){
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td style="width: 35%;">Pick Zip:</td><td> <input id="fromzip" name="fromzip" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Drop Zip:</td><td> <input id="tozip" name="tozip" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Load Type:</td><td> <input id="loadtype" name="loadtype" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Rate:</td><td> <input id="paymentamount" name="paymentamount" maxlength="50"
style="width: 100%;" type="text">
</tr>
</tbody>
</table>
<p style="text-align: center;"><input name="submit" value="Submit"
class="submit" type="submit"></p>
</div>
</form>
</div>
<p style="margin-bottom: -20px;"> </p>
</body>
instead of :
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
use this
echo "<OPTION VALUE='".$row['carriername']."'>".$row['carriername']."</OPTION>";
notice ' in ur value...concate '' to your value attr.. so that it makes it a string....
EDITED
echo "<select name='carriername'>";
while($row=mysql_fetch_array($result)){
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
}
echo "</select>";