I am trying to add an user for the login page but it keeps redirecting even though there is no redirection provided in the code
login.php
<?php
include './connect.php';
session_start();
if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST['username'])) && (!empty($_POST['password']))) {
$postedUsername = $_POST['username'];
$postedPassword = $_POST['password'];
$userDatabaseFind = $database->login->findOne(array('username' => $postedUsername, 'password' => $postedPassword));
$storedUsername = $userDatabaseFind['username'];
$storedPassword = $userDatabaseFind['password'];
if (($postedUsername == $storedUsername) && ($postedPassword == $storedPassword)) {
$_SESSION['username'] = $storedUsername;
if($_SESSION['username'] == 'admin'){
session_regenerate_id();
header('location:printDetail.php');
}
else{
session_regenerate_id();
header('location:welcome.php');
}
} else {
echo 'Error';
}
}
printDetail.php
<?php
require './connect.php';
require './login.php';
//SEARCHING ACROSS THE COLLECTIONS IN THE DATABASE
$printDetailCollections = $database->details;
$printDetailCursor = $printDetailCollections->find();
//INITIALIZE THE VALUE TO ZERO SO IT CAN GO THROUGH THE DATABASE
$i = 0;
//USERNAME AND PASSWORD ARE CHECKED IF IT IS ASSIGNRD THEN THE SESSION IS DISPLAYED
if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {
//LOOP FOR TRAVERSING ACROSS THE DATABASE
foreach ($printDetailCursor as $doc) {
$i++;
}
} else {
header('location:index.html');
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
body{
background-image: url(images/16386858141_65a65879cd_b.jpg);
background-repeat: no-repeat;
background-size: cover;
}
</style>
<script>
$(document).ready(function () {
window.onload = $('#table').hide();
});
</script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Business card Management</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Add entry</li>
<li>Delete entry</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-log-out"></span> Log out</li>
</ul>
</div>
</nav>
<div class="container">
<table id="table">
<thead>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Company Name</th>
<th>Designation</th>
</tr>
</thead>
<tbody>
<?php
foreach ($printDetailCursor as $printDetailDocument) {
?>
<tr>
<td>
<?php
echo (json_decode($printDetailDocument['First Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Second Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Company Name']));
?>
</td>
<td>
<?php
echo (json_decode($printDetailDocument['Designation']));
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
I have 3 collections in the mongodb login and details.The add user tries to access the login collection for adding the user
addUser.php
<?php
include './connect.php';
$username = NULL;
$password = NULL;
$confPassword = NULL;
$passwordError = "";
$userEntryCollection = NULL;
if (isset($_SESSION['username']) && !(empty($_SESSION['username']))) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userEntryCollection = $database->login;
if (isset($_POST['username'])) {
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['confPassword'])) {
$confPassword = $_POST['confPassword'];
}
} else {
echo 'error';
}
if ($password != $confPassword) {
$passwordError = "Your passwords doesnot match";
} else {
$userEntry = array(
"username" => $username,
"password" => $password,
);
$userEntryCollection->insert($userEntry);
}
}
?>
<html>
<head>
<link rel="stylesheet" href=bootstrap-3.3.7-dist/css/bootstrap.min.css>
<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<style>
.col-lg-10{
position: relative;
width: 350px;
border-radius: 25px;
}
form{
position: absolute;
top: 10%;
left: 40%;
}
body{
background-image: url(images/16386858141_65a65879cd_b.jpg);
background-repeat: no-repeat;
background-size: cover;
}
form{
color: black;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav navbar-left">
<li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
</ul>
</nav>
<form method="POST" class="col-lg-10" <?php echo $passwordError; ?> >
<div class="form-group form-inline"><br>
<label for="firstName">Username</label><br>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group form-inline">
<label for="secondName">Password:</label><br>
<input type=password class="form-control" id="password" name="password">
</div>
<div class="form-group form-inline">
<label for="secondName">Confirm Password</label><br>
<input type=password class="form-control" id="confPassword" name="confPassword">
</div>
<button type="button" class="btn btn-success">Submit</button>
</form>
</body>
</html>
i have tried to disable access to the printDetail.php via the URL
and i think that is the main reason for redirection.But couldn't figure out the error in the code
The page gets redirected just by clicking the field of the Username
P.s: i know the password stored this way is insecure
I tryed your code on my server and I confirm your page redirection problem when trying to input something in the addUser.php form...
Your mistake is in these lines:
<nav class="navbar navbar-inverse">
<ul class="nav navbar-nav navbar-left">
<li><a href="printDetail.php"><span class="glyphicon glyphicon-arrow-left">Back</span></li>
</ul>
</nav>
You didn't close the ancor for "Back".
The result is all your form and every single elements of the pages (until another ancor... But there is none) are included by the browser in this ancor. Then also are "Back" links.
It is simple as this.
Use [F12] to debug via the console and code inspector, next time...
;)
Related
I have a stripe payment website where I can make a payment and start a subscription plan that saves to a database, but I would like to know how to make a cancel button that can cancel the subscription.
In thank-you.php, the cancel subscription button exists there, and in paymentProcess.php is the code for the cancel button here if(isset($_POST['cancel'])) {
$stmt = $conn->prepare("SELECT subscription_id FROM users WHERE id = :id");
$stmt->execute(['id' => $customer->id]);
$row = $stmt->fetch(PDO::FETCH_OBJ);
$sub = \Stripe\Subscription::retrieve($row->subscription_id);
$sub->cancel();
}, but nothing changes when clicked, and the stripe log does not change, we are stuck here, any suggestions?
index.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Pricing Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style type="text/css">
.container { margin-top: 100px; }
.card { width: 300px; }
.card:hover {
-webkit-transform: scale(1.05);
-moz-transform: scale(1.05);
-ms-transform: scale(1.05);
-o-transform: scale(1.05);
transform: scale(1.05);
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
}
.price { font-size: 72px }
.currency {
font-size: 25px;
position: relative;
top: -30px;
}
.list-group-item {
border: 0px;
padding: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-header text-center">
<h2 class="price"><span class="currency">$</span>27</h2>
</div>
<div class="card-body">
<h1 class="text-center">Product 1</h1>
<ul class="list-group">
<li class="list-group-item">Feature 1</li>
<li class="list-group-item">Feature 2</li>
<li class="list-group-item">Feature 3</li>
</ul>
</div>
<div class="card-footer text-center">
<form action="paymentProcess.php?pid=1" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_QR1JpboiLh5acjEhK6vwclar00N1Y0Evjd"
data-amount="2700"
data-name="ConnectKitty"
data-description="Catwalk"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
</form>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header text-center">
<h2 class="price"><span class="currency">$</span>67</h2>
</div>
<div class="card-body">
<h1 class="text-center">Product 1</h1>
<ul class="list-group">
<li class="list-group-item">Feature 1</li>
<li class="list-group-item">Feature 2</li>
<li class="list-group-item">Feature 3</li>
</ul>
</div>
<div class="card-footer">
<form action="paymentProcess.php?pid=2" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_QR1JpboiLh5acjEhK6vwclar00N1Y0Evjd"
data-amount="6700"
data-name="ConnectKitty"
data-description="Catwalk"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
</form>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header text-center">
<h2 class="price"><span class="currency">$</span>97</h2>
</div>
<div class="card-body">
<h1 class="text-center">Product 1</h1>
<ul class="list-group">
<li class="list-group-item">Feature 1</li>
<li class="list-group-item">Feature 2</li>
<li class="list-group-item">Feature 3</li>
</ul>
</div>
<div class="card-footer">
<form action="paymentProcess.php?pid=3" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_QR1JpboiLh5acjEhK6vwclar00N1Y0Evjd"
data-amount="9700"
data-name="ConnectKitty"
data-description="Catwalk"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
dashboard.php:
<?php
session_start();
if (!isset($_SESSION['loggedIn'])) {
header('Location: login.php');
exit();
}
$plan = $_SESSION['plan'];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Dashboard</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style type="text/css">
.container { margin-top: 100px; }
.col-md-9 {
border: 1px solid gray;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
img {
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
}
.list-item {
list-style: none;
background: #0088cc;
padding: 8px;
border: 1px solid white;
}
.list-item a {
color: #fff;
}
.list-item:hover {
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
transition: all .3s ease-in-out;
-webkit-transform: scale(1.05);
-moz-transform: scale(1.05);
-ms-transform: scale(1.05);
-o-transform: scale(1.05);
transform: scale(1.05);
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
Log Out
</div>
</div>
<div class="row">
<div class="col-md-3" align="center">
<img src="images/user.png" /><br><br>
<ul class="list-group">
<li class="list-item">
<a href="#">
Feature 1
</a>
</li>
<?php
if ($plan >= 2) {
?>
<li class="list-item">
<a href="#">
Feature 2
</a>
</li>
<?php
}
if ($plan == 3) {
?>
<li class="list-item">
<a href="#">
Feature 3
</a>
</li>
<?php
}
?>
</ul>
</div>
<div class="col-md-9" align="center">
Content
</div>
</div>
</div>
</body>
</html>
paymentProcess.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
$products = array(
"pids" => ["1", "2", "3"],
"1" => "plan_Fkqupc3NdPO5cA",
"2" => "plan_E7Hcdysyy9TU58",
"3" => "plan_E7HdyXFDlTpQpb"
);
if (!isset($_GET['pid']) || !in_array($_GET['pid'], $products['pids']) || !isset($_POST['stripeToken']) || !isset($_POST['stripeEmail'])) {
header('Location: index.php');
exit();
}
require_once('stripe-php-6.24.0/init.php');
$stripe = [
"secret_key" => "sk_test_N62K1YeWqBN1WyEsWmK149Rh00It8OTxqg",
"publishable_key" => "pk_test_QR1JpboiLh5acjEhK6vwclar00N1Y0Evjd",
];
\Stripe\Stripe::setApiKey($stripe['secret_key']);
$pid = $_GET['pid'];
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$customer = \Stripe\Customer::create([
'email' => $email,
'source' => $token,
]);
$sub = \Stripe\Subscription::create([
"customer" => $customer->id,
"items" => [
[
"plan" => $products[$pid],
],
]
]);
$conn = new mysqli("l", "n", "f", "pm");
if(isset($_POST['cancel'])) {
$stmt = $conn->prepare("SELECT subscription_id FROM users WHERE id = :id");
$stmt->execute(['id' => $customer->id]);
$row = $stmt->fetch(PDO::FETCH_OBJ);
$sub = \Stripe\Subscription::retrieve($row->subscription_id);
$sub->cancel();
}
$email = $conn->real_escape_string($email);
$sql = $conn->query("SELECT id FROM users WHERE email='$email'");
if ($sql->num_rows > 0) {
$conn->query("UPDATE users SET plan='$pid' WHERE email='$email'");
$password = "Your Old Password";
} else {
$password = "qwertzuioplkjhgfdsayxcvbnm1234567890";
$password = str_shuffle($password);
$password = strtoupper(substr($password, 0, 10));
$ePassword = password_hash($password, PASSWORD_BCRYPT);
$conn->query("INSERT INTO users (subscription_id, email, plan, password, regDate) VALUES ('$sub', '$email', '$pid', '$ePassword', NOW())");
}
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "f#gmail.com";
$mail->Password = 'rh5GwUXSSK#nf0a';
$mail->Port = 465; //587
$mail->SMTPSecure = "ssl";//tls
$mail->addAddress($email);
$mail->setFrom("f#gmail.com", "f");
$mail->isHTML(true);
$mail->Subject = "Your Login Details...";
$mail->Body = "
Hey,
<br><br>
Thank you for the purchase. Your login details are included below:<br><br>
<b>username</b>: $email<br>
<b>password</b>: $password<br><br>
<a href='http://sweettune.info/StripeRecurringPayments/login.php'>Click Here To Login</a><br><br>
Thanks,<br>
Senaid B.
";
if ($mail->send())
$error = 0;
else
$error = 1;
header('Location: thank-you.php?ue='.$email.'&e='.$error.'&p='.$password.'&pid='.$pid);
?>
login.php:
<?php
session_start();
if (isset($_SESSION['loggedIn'])) {
header('Location: dashboard.php');
exit();
}
$msg = "";
if (isset($_POST['email']) && isset($_POST['password'])) {
$conn = new mysqli("l", "n", "t", "pm");
$email = $conn->real_escape_string($_POST['email']);
$password = $conn->real_escape_string($_POST['password']);
$sql = $conn->query("SELECT id, password, plan FROM users WHERE email='$email'");
if ($sql->num_rows > 0) {
$data = $sql->fetch_assoc();
if (password_verify($password, $data['password'])) {
$_SESSION['plan'] = $data['plan'];
$_SESSION['userID'] = $data['id'];
$_SESSION['loggedIn'] = '1';
header('Location: dashboard.php');
exit();
}
}
$msg = "<span style='color:red'>Please Check Your Login Details</span>";
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Login Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style type="text/css">
.container { margin-top: 100px; }
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6 col-md-offset-3" align="center">
<img src="images/logo.png" /><br><br>
<form method="POST">
<input class="form-control" name="email" placeholder="Email..."><br>
<input class="form-control" name="password" placeholder="Password..." type="password"><br>
<input type="submit" class="btn btn-primary" value="Log In">
</form>
<br><br>
<?php echo $msg ?>
</div>
</div>
</div>
</body>
</html>
logout.php
<?php
session_start();
unset($_SESSION['loggedIn']);
session_destroy();
header('Location: login.php');
exit();
?>
thank-you.php
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Thank You</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style type="text/css">
.container { margin-top: 100px; }
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h2>Thank You For Purchase!!!</h2>
<p>
<?php
if ($_GET['e'] == 1) {
?>
Your login details are included below:<br>
<b>username</b>: <?php echo $_GET['ue'] ?><br>
<b>password</b>: <?php echo $_GET['p'] ?><br><br>
<a href='http://sweettune.info/StripeRecurringPayments/login.php'>Click Here To Login
</a><br><br>
<form action="paymentProcess.php" method="POST" class="pull-right">
<button type="submit" name="cancel" class="btn btn-danger btn-xs">Cancel Subscription</button>
</form>
<?php
} else
echo 'Please Check Your Inbox/SPAM folder!';
?>
</p>
</div>
</div>
</div>
</body>
</html>
I'm having a problem with PHP when trying to use the fopen to write lines of text inside of a textarea to a .txt file. Here's my code, hopefully someone can help!
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
$username = $_SESSION['username'];
$file = "extra/" . $username . ".png";
if (!file_exists($file)) $file = 'extra/defaultProfile.png';
function sendData($u) {
$myfile = fopen('bio/' . $username . '.txt', $u);
}
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>Downloads</li>
<li>Chat</li>
<li>Profile</li>
<li class="logout">Logout</li>
</ul>
</nav>
</div>
</header>
<body>
<div class="profileimg">
<img src=<?php echo $file; ?> width="125" height="125">
</div>
<div class="profilename">
<p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
</div>
<div class="biotext">
<form action="" method="post">
<textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
<input type="submit" name="submit" value="submit" formaction="sendData()"/>
</form>
</div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>
I've tried everything that could come up but I can't seem to find the right solution... I tried making and using a function called sendData() but that didn't work either. Any help anyone can provide is greatly appreciated, thanks!
check if the file exist
check if permission is granted to write the file
sendData() its php and you call it from html
you forgot declare this variable = $u when you call the function
you can do something like this:
<?php
if (!empty($_POST))
{
$text_data = $_POST['text_data'];
$username = $_POST['username'];
fopen('bio/' . $username . '.txt', $text_data);
}
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>Downloads</li>
<li>Chat</li>
<li>Profile</li>
<li class="logout">Logout</li>
</ul>
</nav>
</div>
</header>
<body>
<div class="profileimg">
<img src=<?php echo $file; ?> width="125" height="125">
</div>
<div class="profilename">
<p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
</div>
<div class="biotext">
<form action="" method="post">
<textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
<input type="text" name="text_data" value="">
<input type="hidden" name="username" value="<?php echo $username ?>">
<input type="submit" name="submit" value="submit"/>
</form>
</div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>
in this i wannna get the username who is logged in and display it in the home page when the username is correct and registered in database .should i use session and where to use it.how it is been done
<?php
error_log("chk.php executing");
// Get values from form
include 'config.php';
foreach ($_POST as $key => $value) {
error_log($key);
}
//error_log($_POST['username']);
$username=$_POST['username'];
$password=$_POST['password'];
// Insert data into mysql
$qry = mysql_query("SELECT * FROM useraccount WHERE username='$username'");
if(!$qry) {
die("Query Failed: ". mysql_error());
} else {
$row=mysql_fetch_array($qry);
if ($username==$row['username']) {
if($username=='' || $password=='') {
error_log("some fields are empty");
//header("Location:login.php?id=Some fields are empty");
// header("Content-Type: text/html");
// {echo "<b>Some fields are empty</b>";}
} else if($username==$row['username'] && $password==$row['password']) {
error_log("logged in");
header('Location: home.html');
// header("Content-Type: text/html");
// {echo "<b>User name password verified</b>";}
//header("Location: home.html?id=$username");
} else {
error_log("password is incorrect");
// header("Content-Type: text/html");
// {echo "<b>username already taken or your password is incorrect. Please try again</b>";}
//header("Location:.php?id=username already taken or your password is incorrect. Please try again");
}}
else
error_log("username incorrect");
}
mysql_close();
?>
html,body
{
margin:0px;
height:100%;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 60%;
margin: auto;
}
.content
{
width:100%;
height:400px;
}
.signup
{
height:500px;
}
.footer
{
position:relative;
background-color:black;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>ASK</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="boot.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-6">
<h1>school name</h1>
</div>
<div class="col-md-6">
<img src="../project/photo/l.png" height="150px"/>
</div>
</div>
</div>
</div>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#mynavbar">schoolName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li><a href="about.html" target=_self>About Us</a></li>
<li><a href="infra.html" target=_self>Infrastructure</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="">Administration<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>staff login</li>
<li>staff details</li>
<li>class handling</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<form class="form-horizontal" action="chk.php" method="POST">
<div class="form-group">
<div class="col-xs-3">
<label for="username">Username:</label>
<input name="username" type="username" class="form-control" id="username" placeholder="Enter username">
</div></div>
<div class="form-group">
<div class="col-xs-3">
<label for="pwd">Password:</label>
<input name="password" type="password" class="form-control" id="password" placeholder="Enter password">
</div></div>
<!-- <div class="checkbox">
<label><input type="checkbox"> Remember me</label><br>
</div> -->
<button type="submit" class="btn btn-default">Submit</button><br>
</form>
</div>
<div class="footer navbar-fixed-bottom">
<p> Copyrights# ©WWW.schools.com</p>
</div>
</body>
</html>
first of all when username store in session on login time
<?php
session_start();
- List item
error_log("chk.php executing"); // Get values from form include 'config.php';
foreach ($_POST as $key => $value) { error_log($key); } //error_log($_POST['username']); $username=$_POST['username']; $password=$_POST['password']; // Insert data into mysql $qry = mysql_query("SELECT * FROM useraccount WHERE username='$username'"); if(!$qry) { die("Query Failed: ". mysql_error()); } else {
$row=mysql_fetch_array($qry);
if ($username==$row['username']) {
if($username=='' || $password=='') {
error_log("some fields are empty");
//header("Location:login.php?id=Some fields are empty");
// header("Content-Type: text/html"); // {echo "<b>Some fields are empty</b>";}
} else if($username==$row['username'] && $password==$row['password']) {
error_log("logged in");
$_SESSION['username']=$row['username'];
header('Location: home.html');
// header("Content-Type: text/html"); // {echo "<b>User name password verified</b>";}
//header("Location: home.html?id=$username");
} else {
error_log("password is incorrect");
// header("Content-Type: text/html"); // {echo "<b>username already taken or your password is incorrect. Please try again</b>";}
//header("Location:.php?id=username already taken or your password is incorrect. Please try again");
}}
else
error_log("username incorrect"); } mysql_close(); ?>
Another page we get this username and we change file name home.html to home.php
home.php
echo $_SESSION['username'];
?>
output username print this page
You can use SESSION . You have to use session_start() in every files that you want to display the username
else if($username==$row['username'] && $password==$row['password']) {
$_SESSION['username'] = $username; // store username name in session
error_log("logged in");
header('Location: home.html');
// header("Content-Type: text/html");
// {echo "<b>User name password verified</b>";}
header("Location: home.php?id=$username"); // redirect to home.php page
You can simply display the username by echo $_SESSION['username']; Don't forget to add session_start(); in your home.php. For more info please refer to this link http://php.net/manual/en/function.session-start.php
You should use session_start();
After successfully login,you can post username or email like this-
<?php
include_once'dbconnect.php';
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
//print logged in user's email as saved in database
echo $userRow['email'];
////print logged in user's username as saved in database
echo $userRow['username'];
?>
This is as per my database where-
'dbconnect.php'is connection file.
'users' is tablename.
You can use session_start(); and store the username into it.
//Initialize the session:
session_start();
<!doctype html>
<head>
.
.
.
</head>
<body>
.
.
<?php
$q = "SELECT * FROM useraccount WHERE username='$username'";
$r = $mysqli->query($q);
if ($r->num_rows == 1) {
$_SESSION = $r-> fetch_array(MYSQLI_ASSOC);
}
?>
.
</body>
</html>
You can take a look at my site for that same query at hiteachers.com
The codes for site is given below. Login cannot be authenticated with what I've done. Firstly, it will redirect to the login page as expected if not logged in. Then, after I clearly give the login details correctly, it won't redirect me to the site I want. Instead, it will remain on login page. Please help me...
<!--This is the page that I want to redirect after successful login-->
<?php
session_start();
if($_SESSION['loggedIn'])
{
header('Location: restaurant.php');
}
else
{
header('Location: login.php');
}
?>
<html lang="en">
<head>
<title>Welcome to Foodline</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
<link href="css/simple-sidebar.css" rel="stylesheet">
<script src="bootstrap/js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<style>
/* Remove the jumbotron's default bottom margin */
.jumbotron {
margin-bottom: 0;
}
/* Add a gray background color and some padding to the footer */
footer {
background-color: #f2f2f2;
padding: 25px;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container text-center">
<h1><font face="Analecta">FOODLINE</font></h1>
<p>We provide the best service for our costumers</p>
</div>
</div>
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand"><font face="Analecta" color="white">>Restaurants<</font></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li>
Hamro Didi (HD)
</li>
<li>
HK
</li>
<li>
Junu Hotel
</li>
<li>
Junction Cafe
</li>
<li>
Laxmi Hotel
</li>
</ul>
</div>
</div>
</nav>
<footer class="container-fluid text-center">
<p>Foodline Official Website ©</p>
<p align="center">Logged in as: <div id="username" align="center"> <span class="glyphicon glyphicon-log-in"></span><?php
if(isset($_GET['id'])){
echo ' '.$_GET['id'];
}
else {
echo '(write) a 404 page';
}
?>
</div>
</p>
</footer>
</div>
<!--This is login.php-->
<?php
//session_start();
include("connection.php");
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from form
$username = $_POST['username'];
$password = $_POST['password'];
// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
//Input Validations
if($username == '') {
$_SESSION["login_user"] = $username; $msg = "Username missing";
header("location: login.php?msg=$msg");
}
if($password == '') {
$msg = "Password missing";
header("location: login.php?msg=$msg");
}
//Create query
$qry="SELECT * FROM user WHERE user_name='$username' AND user_password='$password'";
$result =mysql_query($qry)or die(mysql_error());
$output=mysql_fetch_assoc($result);
//Check whether the query was successful or not
if(!empty($output)) {
//Login Successful
$_SESSION['name']= $username;
$_SESSION['loggedIn'] = true;
header("location:restaurant.php?id=$username");
}
else {
//Login failed
$msg= "user name and password not found";
header("location:login.php?msg=$msg");
}
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
<script src="bootstrap/js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<style>
.jumbotron {
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container text-center">
<h1><font face="Analecta">FOODLINE</font></h1>
<p>We provide the best service for our costumers</p>
</div>
</div>
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.php">Logo</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Restaurants</li>
<li>Contact</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-user"></span> Sign Up</li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h2><font face="Analecta">>Login from here<</font></h2>
<form role="form" name="login" action="login.php" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" placeholder="Enter username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" placeholder="Enter password" required>
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button type="submit" class="btn btn-default" value="login">Submit</button>
<br>
<br>
<?php
$msg = (isset($_GET['msg']) ? $_GET['msg'] : null); //GET the message
if($msg!='') echo '<p>'.$msg.'</p>'; //If message is set echo it
?>
</form>
<p>Not a user yet? Sign up here</p>
</div>
<footer class="container-fluid text-center">
<p>Foodline Official Website ©</p>
<p>Get deals:
<span class="glyphicon glyphicon-menu-right"></span>SignUp
</p>
</footer>
</body>
</html>
Uncomment:
//session_start();
From line 5 in login.php and change to this:
if(! $_SESSION['loggedIn']) {
header('Location: login.php');
}
in restaurant.php.
I have the following code:
<?php session_start(); ?>
<!DOCTYPE HTML>
<html>
<head>
<title> Admin Login </title>
<link rel="stylesheet" href="../bootstrap.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<style>
body {
padding:40px;
}
</style>
</head>
<body>
<div class="container">
<?php
if(!isset($_SESSION['AdminAc'])) {
;
if(isset($_POST['login'])) {
require("../src/config.php");
if($AdminUsername != $_POST['username']) {
$error = "Incorrect Username";
} elseif($AdminPassword != $_POST['password']) {
$error = "Incorrect Password";
} elseif($AdminPassword != $_POST['password'] && $AdminUsername != $_POST['Username']) {
$error = "Incorrect Cridentials";
} else {
$_SESSION['AdminAc'] = true;
header("Location: ".$_SERVER['PHP_SELF'].""); exit();
}
}
?>
<br />
<h4> Admin Login </h4>
<?php if(isset($error)) { ?> <div class="alert alert-danger" style="width:45%;"> <?php echo $error; ?> </div> <?php } ?>
<form action="" method="POST">
<input type="text" name="username" placeholder="Admin Username"/>
<input type="password" name="password" placeholder="Admin Password"/> <br />
<input type="submit" name="login" class="btn btn-success" value="Continue"/>
</form>
</body>
</html>
<?php
} else {
require('../src/config.php');
?>
<div class="container">
<div style="float:left; margin:10px;">
Hello, <strong><?php echo ucfirst($AdminUsername); ?></strong>
| Logout
<br /><br />
<ul class="nav nav-pills nav-stacked">
<li> <i class="icon-unlock-alt"></i> Change Password </li>
<li> <i class="icon-user"></i> Add User </li>
</ul>
</div>
<div class="well" style="text-align:center; margin: 0 auto; overflow:auto;">
<h3> Admin Panel </h3>
<hr />
<table align=center class="table">
<?php
$q = $con->query("SELECT * FROM users");
while($qq = $q->fetch_object()) {
echo "<tr> <td align=left style='padding-right:10px;''> ".$qq->username."</td> <td align=right> <a href='?delete=".$qq->ID."'> Delete </a> </td> </tr> <br />";
}
if($q->num_rows < 1) {
echo "<div style='text-align:center;'> No users exist in the database</div>";
}
?>
</table>
</div>
<?php
if(isset($_GET['delete'])) {
$ID = $_GET['delete'];
$con->query("DELETE FROM users WHERE ID='$ID'");
header("Location: ".$_SERVER['PHP_SELF'].""); exit();
}
if(isset($_GET['logout'])) {
session_destroy();
header("Location: ".$_SERVER['PHP_SELF'].""); exit();
}
}
My problem is specifically with the following lines of code:
<div class="well" style="text-align:center; margin: 0 auto; overflow:auto;">
<h3> Admin Panel </h3>
<hr />
<table align=center class="table">
<?php
$q = $con->query("SELECT * FROM users");
while($qq = $q->fetch_object()) {
echo "<tr> <td align=left style='padding-right:10px;''> ".$qq->username."</td> <td align=right> <a href='?delete=".$qq->ID."'> Delete </a> </td> </tr> <br />";
}
if($q->num_rows < 1) {
echo "<div style='text-align:center;'> No users exist in the database</div>";
}
?>
</table>
</div>
On every new table entry, the table goes down the page and leaves a huge gap with the top like on this screenshot I made http://prntscr.com/1o9zbl
Can anyone help me fix this issue?
Why do you have the <br/> at the end of the echo-statement. Removing this should fix your issue.