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.
Related
I hope you can give me a lead how uploading my profile.php suddenly made it invalid to the server to proceed (?). I'm running my website online on Strato.de so I can check out if it works in a live environment. I renamed the file from reset.php to profile.php and suddenly became unable to be reached.
Test account: id: user | pw: test123
profile.php
<?php
session_start();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title id="txt_white">Welcome</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" href="/styles.css">
<style type="text/css">
body{ font: 14px sans-serif; text-align: center; }
</style>
</head>
<body>
<div>
<ul class="navfont">
<li>Home</li>
<li>Login</li>
</ul>
<div class="date">
<?php echo "Last Update: " . date("d/m/Y h:i:sa"); ?>
</div>
</div>
<div class="page-header">
<h1 id="txt_white">Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></h1></b>. <p>Edit account</p>
</div>
<p id="txt_white">
Reset Your Password
Sign Out of Your Account
</p>
</body>
</html>
index.php
<?php
session_start();
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: home.php");
exit;
}
require_once "config.php";
$username = $password = "";
$username_err = $password_err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["username"]))){
$username_err = "Please enter username.";
} else{
$username = trim($_POST["username"]);
}
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
if(empty($username_err) && empty($password_err)){
$sql = "SELECT id, username, password FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = $username;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
header("location: home.php");
} else{
$password_err = "The password you entered was not valid.";
}
}
} else{
$username_err = "The username you entered was not valid.";
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
mysqli_stmt_close($stmt);
}
}
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Login</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<link rel="stylesheet" href="styles.css"/>
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<body>
<div>
<ul class="navfont">
<li>Home</li>
</ul>
<div class="date">
<?php echo "Last Update: " . date("d/m/Y h:i:sa"); ?>
</div>
</div>
<div class="wrapper">
<h2 id="txt_white">Login</h2>
<p id="txt_white">Please fill in your credentials to login.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div id="txt_white" class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username</label>
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div id="txt_white" class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" name="password" class="form-control">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Login">
</div>
</form>
</div>
</body>
</html>
home.php
<?php session_start(); ?>
<?php
if(!isset($_SESSION['id'])){
die(header("location: 404.php"));
}
?>
<!DOCTYPE html>
<html lang="de-DE">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="styles.css"/>
<link rel=stylesheet href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Home</title>
</head>
<body>
<div>
<ul class="navfont">
<li>Home</li>
<div class="btn float-right">
Accountsettings
Sign Out
</div>
</ul>
<div class="date">
<?php echo "Last Update: " . date("d/m/Y h:i:sa"); ?>
</div>
</div>
<div class="hello">
<h1>Welcome, <?php echo htmlspecialchars($_SESSION["username"]); ?></h1>
</div>
<div class="container">
<h2>Verzeichnis-Browser</h2>
<p>Ein Abbild vom Verzeichnis-Browser //Upload-Funktion kommt noch</p>
<table class="table table-hover">
<thead>
<tr>
<th>Filename</th>
<th>Last Change</th>
<th>Filesize</th>
</tr>
</thead>
<tbody>
<tr>
<td>test.docx</td>
<td>01.12.2020 04:22</td>
<td>23 KB</td>
</tr>
<tr>
<td>teller.xml</td>
<td>12.12.2020 14:11</td>
<td>41 MB</td>
</tr>
</tbody>
</table>
</div>
<footer class="footer">Copyright 2020</footer>
</body>
</html>
In my database, I have a table column that has a filepath for images. The filepath's name is photo Some of my rows don't have a filepath. Inside my while loop forshowing the table, I would like to add a condition that if there is no filepath, it would prompt the text "User did not upload a photo yet." and when it has filepath, I can show the filepath and link it with a target blank.
This is my php file for it.
<?php require_once 'process.php';
session_start();
$role = $_SESSION['sess_userrole'];
$name = $_SESSION['sess_name'];
if(!isset($_SESSION['sess_username']) && $role!="admin"){
header('Location: index.php?err=2');
}
?>
<html>
<head>
<title>User Accounts</title>
<link rel="icon" href="isalonlogo.png">
<link rel="stylesheet" href="css2/bootstrap.min.css">
<script src="css/jquery-3.3.1.slim.min.js"></script>
<script src="css/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="css/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_SESSION['message'])):?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset ($_SESSION['message']);?>
</div>
<?php endif ?>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<span class="navbar-brand" href=""><?php echo " " . "$name"?>, here are the Stylist user lists.</span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>User Lists</li>
<li><?php echo $_SESSION['sess_username'];?></li>
<li>Logout</li>
</ul>
</div>
</div>
</div>
<br> <br> <br>
<div class="container">
<?php
$mysqli = new mysqli("localhost","id7508046_root","123123123as","id7508046_isalon") or die(mysqli_error($mysqli));
$result = $mysqli->query("SELECT * FROM stylist ") or die($mysqli->error);
?>
<div class="row justify-content-center" width="80%">
<table class="table">
<thead>
<tr>
<th>UserName</th>
<th>Name</th>
<th>Image</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['name'] ?></td>
// THIS PART HERE IS THE PROBLEM
<td><?php if(strcmp($row['photo'],"") == 0): {echo 'Ola';
}else:
{echo '<img src="'.$row['photo'].'">';
}
endif;
?></td>
<td>
<a href="userlist.php?edit=<?php echo $row['stylist_id']; ?>"
class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row['stylist_id']; ?>"
class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
function pre_r($array){
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
<br>
<br>
<div class="container justify-content-center">
<h5 class=" justify-content-center">Admin <?php echo $name;?>, create or edit an account here.</h5>
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label>UserName</label>
<input type="text" name="username" class="form-control" value="<?php echo $username;?>" placeholder="Enter new user Username" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="text" name="password" class="form-control" value="<?php echo $password;?>" placeholder="Enter new user Password" required>
</div>
<div class="form-group">
<?php
if($update == TRUE): ?>
<button type="submit" class="btn btn-primary" name="update">Update</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
endif; ?>
</div>
</form>
</div>
</div>
</body>
Try
<td>
<?php if(!empty(trim($row['photo']))): ?>
<img src="<?php echo $row['photo'];?>"/>
<?php else: ?>
User did not upload a photo yet
<?php endif; ?>
</td>
Use below code for TD as you currently added same code in if and else both instead of using if else you can use the ternary operator.
<td>
<?php $photo = (isset($row['photo'])) ? $row['photo'] : "";
echo ($photo) ? "'. <img src=".$photo."> .'" : ""; ?>
</td>
I am getting an error when I try to edit/update my table. It's weird because I can insert new data into the database fine and I can delete it afterwards fine. I only have problems when editing/updating the data.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id='1'' at line 6' in C:\Program Files (x86)\Ampps\www\Gats\editargat.php on line 64
This is the code:
<?php
error_reporting( ~E_NOTICE );
require_once 'dbconn.php';
if(isset($_GET['edit_id'])){
$id = $_GET['edit_id'];
$editar = $conn->prepare('SELECT nom, sexe, edat, foto FROM TaulaGats WHERE id=:uid');
$editar->execute(array(':uid'=>$id));
$edit_row = $editar->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
}
else{
header("Location: index.php");
}
if(isset($_POST['btn_save_updates'])){
$nom = $_POST['nom'];
$sexe = $_POST['sexe'];
$edat = $_POST['edat'];
$foto = $_FILES['imatge']['name'];
$carpeta_tmp = $_FILES['imatge']['tmp_name'];
$imatge_mida = $_FILES['imatge']['size'];
if($foto){
$carpetaImatges = 'imatges/';
$imatge_ext = strtolower(pathinfo($foto, PATHINFO_EXTENSION));
$extensions_vàlides = array('jpeg', 'jpg', 'png', 'gif');
$pic = rand(1000,1000000).".".$imatge_ext;
if(in_array($imatge_ext, $extensions_vàlides)){
if($imatge_mida < 5000000){
unlink($carpetaImatges.$edit_row['foto']);
move_uploaded_file($carpeta_tmp, $carpetaImatges.$pic);
}
else{
$error = "La foto és massa gran.";
}
}
else{
$error = "Només les extensions JPG, JPEG, PNG & GIF són admeses.";
}
}
else{
$pic = $edit_row['foto'];
}
if(!isset($error)){
$prepIexec = $conn->prepare('UPDATE TaulaGats
SET nom=:unom,
sexe=:usexe,
edat=:uedat,
foto=:ufoto,
WHERE id=:uid');
$prepIexec->bindParam(':unom', $nom);
$prepIexec->bindParam(':usexe', $sexe);
$prepIexec->bindParam(':uedat', $edat);
$prepIexec->bindParam(':ufoto', $pic);
$prepIexec->bindParam(':uid', $id);
if($prepIexec->execute()){
header("refresh:1;index.php");
}
else{
$error = "No s'ha pogut editar.";
}
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gats</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="estil_2.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="jquery-1.11.3-jquery.min.js"></script>
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1 class="h2">Editar<a class="btn btn-default" href="index.php">Veure llista gats</a></h1>
</div>
<div class="clearfix"></div>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
if(isset($error)){
?>
<div class="alert alert-danger">
<?php echo $error; ?>
</div>
<?php
}
?>
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Nom</label></td>
<td><input class="form-control" type="text" name="nom" value="<?php echo $nom; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Sexe</label></td>
<td><input class="form-control" type="text" name="sexe" value="<?php echo $sexe; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Edat</label></td>
<td><input class="form-control" type="text" name="edat" value="<?php echo $edat; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Foto</label></td>
<td>
<p><img src="imatges/<?php echo $foto; ?>" height="150" width="200" /></p>
<input class="input-group-edit" type="file" name="imatge" accept="image/*" />
</td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-default btn-guardar">
Guardar
</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Index.php:
<?php
require_once 'dbconn.php';
if(isset($_GET['eliminar_id']))
{
$prepIexec = $conn->prepare('SELECT foto FROM TaulaGats WHERE id =:uid');
$prepIexec->execute(array(':uid'=>$_GET['eliminar_id']));
$imatgeRow = $prepIexec->fetch(PDO::FETCH_ASSOC);
unlink("imatges/".$imatgeRow['foto']);
$eliminar = $conn->prepare('DELETE FROM TaulaGats WHERE id =:uid');
$eliminar->bindParam(':uid', $_GET['eliminar_id']);
$eliminar->execute();
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Gats</title>
<link rel="stylesheet" href="estil_index.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>Gats en adopció
<a class="btn btn-default" href="afegirgat.php"> + Afegir nou gat </a>
</h1>
</div>
<br />
<div class="row">
<?php
$prepIexec = $conn->prepare('SELECT id, nom, sexe, edat, foto FROM TaulaGats ORDER BY id ASC');
$prepIexec->execute();
if($prepIexec->rowCount() > 0)
{
while($row = $prepIexec->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<div class="col-xs-3">
<p class="page-header infoGats_nom"><?php echo $nom ?></p>
<p class="infoGats">
<?php echo $sexe." / ".$edat; ?>
</p>
<img src="imatges/<?php echo $row['foto']; ?>" class="img-rounded" width="250px" height="200px" />
<p class="page-header">
<span>
<a class="btn btn-info" href="editargat.php?edit_id=<?php echo $row['id']; ?>" title="click per editar"><span class="glyphicon glyphicon-edit"></span> Editar </a>
<a class="btn btn-success" href="mouregat.php?moure_id=<?php echo $row['id']; ?>" title="click per moure"><span class="glyphicon glyphicon-edit"></span> Moure </a>
<a class="btn btn-danger" href="?eliminar_id=<?php echo $row['id']; ?>" title="click per eliminar" onclick="return confirm('Estàs segur que vols el·liminar aquestes dades?')"><span class="glyphicon glyphicon-remove-circle"></span> El·liminar </a>
</span>
</p>
</div>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
No hi ha gats per adoptar.
</div>
</div>
<?php
}
?>
</div>
</div>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
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...
;)
When i try to go to the home page, the page becomes blank and it is because of the session tags because when i remove them the page shows.
Home page:
<?php
session_start();
include 'includes/db_connect.php';
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="styles/home.css" type="text/css" >
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript" src="js/nav.js" ></script>
<script type="text/javascript">
<!--
function toggle_visibility() {
var e = document.getElementById("nav");
if(e.style.display == 'table')
e.style.display = 'none';
else
e.style.display = 'table';
}
//-->
</script>
</head>
<body>
<div class="Menu" >
<div class="middle" >
<div class="profilepic" >
<a href="profile.php" >
<img src="" ></img>
</a>
</div>
<div class="search" >
<form method="POST" action="search.php" >
<input type="search" id="search-input" class="search-input" placeholder="Please enter a search term!" minlength="1" >
</form>
</div>
<p><?php echo '$_SESSION["Username"]'; ?></p>
</div>
<div class="navigation">
<div class="openMenu" id="openMenu" ><button onclick="toggle_visibility();">Menu</button></div>
<ul id="nav" >
<li><a href="profile.php" ><i id="navicon" class="fa fa-user fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-envelope-o fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-bars fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-group fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-rss fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-clock-o fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-edit fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-gear fa-spin fa-2x" ></i></a></li>
</ul>
</div>
</div>
<div class="status" >
<form action="" method="GET" >
<div class="upload" >
<input type="file" name="videofilename" accept="video/*" class="upload" />
</div>
<div class="upload">
<input type="file" name="audiofilename" accept="audio/*" class="upload" />
</div>
<div class="upload" >
<input type="file" name="imagefilename" accept="image/*" />
</div>
<textarea class="statusText" id="statusText" rows="1" cols="60" placeholder="Update Your Status..." ></textarea>
<input type="submit" value="Post!" >
</form>
</div>
</body>
</html>
This is my login page where the session is stored:
<?php
session_start();
include 'includes/db_connect.php';
?>
<html>
<head>
<title>Howlers | Login</title>
</head>
<body>
<div class="login" >
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
header('Location=home.php');
?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysqli::real_escape_string($_POST['username']);
$password = md5(mysqli::real_escape_string($_POST['password']));
$checklogin = mysqli::query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysqli::num_rows($checklogin) == 1)
{
$row = mysqli::fetch_array($checklogin);
$email = $row['Email'];
$_SESSION['Username'] = $username;
$_SESSION['Email'] = $email;
$_SESSION['LoggedIn'] = 1;
header('Location=home.php');
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href='login.php'>click here to try again</a>.</p>";
}
}
else
{
?>
<h1>Login</h1>
<p>Thanks for visiting! Please either login below, or click here to register.</p>
<form method="post" action="login.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="submit" id="submit" value="Login" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
here is a picture of the outcome:
http://prntscr.com/5580lh
I am not sure what i am doing wrong here can anyone please help me :(?
one of error is the redirect in the login-page:
you write header('Location=home.php');
The equal is wrong, use the double-point: header('Location:home.php');
You didn't close the if condition proper in home page:
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
}
you forgot to close the if condition which was fatal error!
Make following changes in your pages
Home Page
<?php
session_start();
include 'includes/db_connect.php';
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
}
?> ...
Login Page
...
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
header('Location:home.php');
?>
<?php
}
?>
...
and
...
if(mysqli::num_rows($checklogin) == 1)
{
$row = mysqli::fetch_array($checklogin);
$email = $row['Email'];
$_SESSION['Username'] = $username;
$_SESSION['Email'] = $email;
$_SESSION['LoggedIn'] = 1;
header('Location:home.php');
}
...