Using sessions to display a users name on website - php

Below is the code for the home page (index.php), at the top of the home page, I want the user to be able to login, I have currently got the form set up but I am wondering how I will display this on other pages.
Here is the code:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Sean Coyne's Food Shop</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<link rel="icon" type="image/x-icon" href="images/favicon.ico" />
<meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>
<body>
<div id="page">
<div id="logo">
<img src="images/logo.jpg" alt="Sean Coyne's Food Shop"
title="Sean Coyne's Food Shop" width="400px" height="70px"/>
<div id="login">
<form action="loggedin.php" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
</form>
</div>
</div>
<div id="nav">
<div id="menu">
<ul>
<li>Home</li>
<li>Food</li>
<li>Drink</li>
<li>About Us</li>
<li>Where to find us</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="main">
<h1>Welcome to Sean Coyne's Food Shop</h1>
</div>
</div>
</body>
</html>
Below is an example of another page (loggedin.php) on the website, it is still the homepage but I don't want the form to be displayed, instead I want the users name. Here is the code:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Sean Coyne's Food Shop</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<link rel="icon" type="image/x-icon" href="images/favicon.ico" />
<meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>
<body>
<div id="page">
<div id="logo">
<img src="images/logo.jpg" alt="Sean Coyne's Food Shop"
title="Sean Coyne's Food Shop" width="400px" height="70px"/>
<div id="login">
<?php
echo $_SESSION['username'];
?>
</div>
</div>
<div id="nav">
<div id="menu">
<ul>
<li>Home</li>
<li>Food</li>
<li>Drink</li>
<li>About Us</li>
<li>Where to find us</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="main">
<h1>Welcome to Sean Coyne's Food Shop</h1>
</div>
</div>
</body>
</html>

You can use a conditional to check is the session username is set, and if it is, then you hide the login form. Something like:
<?php if (isset($_SESSION['username']): ?>
<h1>You are logged in!</h1>
<?php else: ?>
<form>
...
</form>
<?php endif; ?>

Related

How to change pages without changing url?

I am currently working on a project, but I'm just stuck on something. I know there are a lot of questions related to this, but I couldn't find any useful information.
I'm new to PHP and I need to write php code which changes pages and doesn't change the URL (i.e if I am going to login.php, the URL should still be home.php) and I need to use the GET method.
I'm sorry if there are some mistakes in my code, but thanks for any help.
Here is my home.php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="Layout.css" />
<link rel="stylesheet" type="text/css" href="Menu.css" />
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8" />
<title> Title </title>
</head>
<body>
<div id="Holder"></div>
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li> Home </li>
<li>Login </li>
<li>Register </li>
</ul>
</nav>
</div>
<div id="Content">
<div id="PageHeading">
<h1> Welcome to HOME page </h1>
</div>
</div>
<div id="Footer"></div>
</body>
</html>
Here is my login.php file:
<?php
session_start();
$db = mysqli_connect ("localhost", "root", "","information1");
if (isset($_POST['Register'])){
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$sql = "SELECT username, password FROM user WHERE username='$username' AND
password='$password'";
$base =mysqli_query ($db, $sql);
if (mysqli_num_rows($base) == 1) {
header ("location: nav_menu.php");
}
else
{
echo "Passwords does not match";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Layout.css" />
<link rel="stylesheet" type="text/css" href="Menu.css" />
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8" />
<title> Title </title>
</head>
<body>
<div id="Holder"></div>
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li> Home </li>
<li>Login </li>
<li>Register </li>
</ul>
</nav>
</div>
<div id="Content">
<div id="PageHeading">
<h1> Welcome to HOME page </h1>
</div>
<div id="ContentRight">
<h2> Text2 </h2> </br>
<h6> Text3 </h6 </br>
</div>
<div id="ContentLeft">
<form name ="form2" method="POST" action="login.php">
<div class = "ContentTable">
<table width="400" border="0" align ="left">
<tbody>
<h4> Username: </h4>
<input type="text" name= "username" id="username" required></td>
</tr>
<tr>
<td> </td>
</tr>
<h4> Password: </h4>
<input type="text" name= "password" id="password" required></td>
</tr>
<td><input type="submit" name="Register" id="RegisterButton" value="Register"></td>
</div>
</div>
</form>
<div id="Footer"></div>
</body>
</html>

Displaying users name on every page using sessions

I am currently creating a website that will allow the user to log in using a username only, no password is required. Once the user has typed their name into the form, their name should then be placed on all of the pages they then visit until they log out.
The problem/s I am facing is that the username is not showing on the other pages once logged in. Instead I have been getting problems such as errors (Notice: Undefined index: username in /ceri/homes1/s/sec17/public_html/cs25010/home.php on line 41) and the nothing showing up at all.
Here is the code for the login page:
<?php
session_save_path("/aber/sec17/public_html/cs25010/tmp");
session_start();
if (empty($_SESSION['username'])) {
if (isset($_POST['submit'])) {
$_SESSION["username"] = $_POST["username"];
header("Location: home.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sean Coyne's Food Shop</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<link rel="icon" type="image/x-icon" href="images/favicon.ico" />
<meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>
<body>
<div id="page">
<div id="logo">
<img src="images/logo.jpg" alt="Sean Coyne's Food Shop"
title="Sean Coyne's Food Shop" width="400px" height="70px"/>
</div>
<div id="nav">
<div id="menu">
<ul>
<li>Home</li>
<li>Products</li>
<li>Offers</li>
<li>About Us</li>
<li>Where to find us</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="main">
<h1>Welcome to Sean Coyne's Food Shop</h1>
<h2>Please Log In below:</h2>
<br></br>
<div id="login">
<?php
echo '<form action="home.php" method"post">
<input type="text" name="username" text="input username"
placeholder="Username" required>
<input type="submit" name="submit" value="submit" />
</form>';
?>
</div>
</div>
</div>
</body>
</html>
And here the code for the home page: (I will not be placing the username here when its finished, this is just while im testing it to see if its working)
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<link rel="icon" type="image/x-icon" href="images/favicon.ico" />
<meta name="description" content="Welcome to Sean Coyne's Food Shop" />
</head>
<body>
<div id="page">
<div id="logo">
<img src="images/logo.jpg" alt="Sean Coyne's Food Shop"
title="Sean Coyne's Food Shop" width="400px" height="70px"/>
</div>
<div id="nav">
<div id="menu">
<ul>
<li>Home</li>
<li>Products</li>
<li>Offers</li>
<li>About Us</li>
<li>Where to find us</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="main">
<h1>Welcome to Sean Coyne's Food Shop</h1>
<?php
echo $_SESSION['username'];
?>
</div>
</div>
</body>
</html>
Heres what it currently look like when you log in:
You have a typo. Otherwise default method for form method is get.
Change:
<form action="home.php" method"post">
To this:
<form action="home.php" method="post">

favicon will not show up after I point my noip to my domain name

My favicon will not show up on three different web browsers opera, Firefox, and Chrome and I have cleared cookies and cache so it’s not that. This started happening after I pointed my noip my domain name. What did I do wrong? How can I fix it?
http://404tool.dns.net has the favicon
http://404tool.com favicon is gone
<?php
session_start();
include('inc/config.php');
include('inc/functions.php');
BlockIP();
LoggedInTrue();
?>
<html>
<head>
<title>404Tool - Home</title>
<link rel="icon"type="image/ico"href="img/favicon.ico">
<link rel="stylesheet" href="css/main.css" type="text/css"
media="screen" />
</head>
<body>
<div id="wrapper">
<div id="sidebar">
<div id="menu">
<div id="menu-header">
<div id="title">
<span>404</span>Tools
</div>
</div>
<div id="menu-account">
<div id="account-info">
<table>
<tr>
<td>User</td>
<td><span><?php echo $_SERVER['REMOTE_ADDR']; ?></span></td>
</tr>
<tr>
<td>Level</td>
<td><span>Guest</span></td>
</tr>
</table>
</div>
</div>
<div id="menu-menu">
<div class="menu-news">
<h2 class="current">Home</h2>
</div>
<div id="menu-login">
<ul>
<li id="loginstrip"class="current">Login</li>
<div id="dropdown">
<form method="POST">
<div id="dropdown-strip">
<div id="dropdown-text">
<input type="text" name="username" placeholder="Username" autocomplete="off" />
</div>
</div>
<div id="dropdown-strip">
<div id="dropdown-text">
<input type="password" name="password" placeholder="Password" autocomplete="off" />
</div>
</div>
<div id="dropdown-strip2">
<div id="dropdown-text">
<span id="loginbutton"><input type="submit" value="Login" name="login" /></span>
</div>
</div>
<?php Login(); ?>
</form>
</div>
</ul>
</div>
</div>
</div>
</div>
<div id="main-news">
<div id="main-header">
<div id="main-title">
Home
</div>
</div>
<div id="main-content-news">
This is a Private Service and accounts are created by filling out the form below. Please wait 48 hours for the account to be created before submitting another account request!
</div>
<div id="main-content-news">
<div id="box">
<br />
<br />
<div id="box-header">
</div>
<div id="box-content">
<div id="box-title">
</div>
</div>
<div id="box-content">
It currently looks like you are serving different version of the app. Look at the source code from each of those sites and you'll see that 404tools.com does not have a favicon link in the <head>...</head>
http://404tools.ddns.net/:
<head>
<title>404Tools - Home</title>
<link rel="icon" type="image/ico" href="img/favicon.ico">
<link rel="stylesheet" href="css/main.css" type="text/css" media="screen">
</head>
http://404tools.com/
<head>
<title>404 tools</title>
<meta name="keywords" content="email bomber, skype resolver, skype, 404, tools, sms bomber, geo location, ip logger,">
<meta name="description" content="">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css"></style>
</head>

how to fetch images from database through image id

I have done a gallery page by fetching images from a database. Now I have to view a particular image in that page. For that I need to set id for the particular image to view. What is the code to view image through id?
<!doctype html>
<!--[if IE 7 ]> <html lang="en-gb" class="isie ie7 oldie no-js"> <![endif]-->
<!--[if IE 8 ]> <html lang="en-gb" class="isie ie8 oldie no-js"> <![endif]-->
<!--[if IE 9 ]> <html lang="en-gb" class="isie ie9 no-js"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en-gb" class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Image Dental Clinic</title>
<meta name="description" content="">
<meta name="author" content="">
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- **Favicon** -->
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<!-- **CSS - stylesheets** -->
<link id="default-css" href="style.css" rel="stylesheet" media="all" />
<link id="shortcodes-css" href="shortcodes.css" rel="stylesheet" media="all" />
<link id="skin-css" href="skins/green/style.css" rel="stylesheet" media="all" />
<link id="fancy-box" href="css/jquery.fancybox.css" rel="stylesheet" media="all" />
<link href="css/prettyPhoto.css" rel="stylesheet" media="all" />
<!-- **Additional - stylesheets** -->
<link href="css/responsive.css" rel="stylesheet" media="all" />
<link href="css/pace-theme-loading-bar.css" rel="stylesheet" media="all" />
<!-- **Font Awesome** -->
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/flaticon.css">
<!-- **Google - Fonts** -->
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Crete+Round' rel='stylesheet' type='text/css'>
<!-- **Modernizr** -->
<script src="js/modernizr.custom.js"></script>
<script type="text/javascript">
var mytheme_urls = {
stickynav : 'enable'
};
</script>
</head>
<body>
<!--<div id="loader-wrapper">
<div class="loader">
<span class="glyph-icon flaticon-man159"></span>
</div>
</div>-->
<!-- **Wrapper** -->
<div class="wrapper">
<div class="inner-wrapper">
<!-- header-wrapper starts here -->
<div id="header-wrapper">
<header id="header">
<!-- Top bar starts here -->
<div class="top-bar">
<div class="container">
<div class="dt-sc-contact-info">
<p><i class="fa fa-phone"></i>Any Questions? Call Us: <span>966 1 4836060</span></p>
</div>
<!-- <div class="top-right">
<ul>
<li><a title="Login" href="login.html"><span class="fa fa-sign-in"></span>Member Login</a></li>
<li><a title="Register Now" href="register.html"><span class="fa fa-user"></span> Register </a></li>
</ul>
</div>-->
</div>
</div>
<!-- Top bar ends here -->
<div class="main-menu-container">
<div class="main-menu">
<div id="logo"> <a title="Travel" href="index.html"><img title="Fitness" alt="Fitness" src="images/logo.png"></a> </div>
<div id="primary-menu">
<div class="dt-menu-toggle" id="dt-menu-toggle">Menu<span class="dt-menu-toggle-icon"></span></div>
<nav id="main-menu">
<ul id="menu-main-menu" class="menu">
<li class="menu-item-simple-parent menu-item-depth-0">Home </li>
<li class="menu-item-megamenu-parent megamenu-4-columns-group menu-item-depth-0"> About Us
</li>
<li class="menu-item-simple-parent menu-item-depth-0">Our Services
<ul class="sub-menu">
<li> Cosmetic Dentistry </span> </span> </li>
<li> Dental Implants </span> </span> </li>
<li> Periodontal Care</span> </span> </li>
<li> Endodontic Treatment </span> </span> </li>
<li> Pediatric Dentistry </span> </span> </li>
<li> Orthodontics </span> </span> </li>
<li> Restorative Dentistry </span> </span> </li>
</ul>
<a class="dt-menu-expand">+</a> </li>
<li class="menu-item-megamenu-parent megamenu-4-columns-group menu-item-depth-0"> Doctors </li>
<li class="menu-item-megamenu-parent megamenu-4-columns-group menu-item-depth-0"> Appoinment </li>
<li class="current_page_item menu-item-megamenu-parent megamenu-4-columns-group menu-item-depth-0"> Gallery </li>
<li class="menu-item-simple-parent menu-item-depth-0">Contact Us
</li>
</ul>
</nav>
</div>
</div>
</div>
</header>
</div>
<!-- header-wrapper ends here -->
<!-- breadcrumb starts here -->
<div class="breadcrumb-wrapper">
<div class="container">
<h1>Gallery</h1>
<div class="breadcrumb">
Home
<span class="default"> </span>
<h4>Gallery</h4>
</div>
</div>
</div>
<!-- breadcrumb ends here -->
<div id="main">
<!-- main-content starts here -->
<div id="main-content">
<section id="primary" class="content-full-width">
<div class="dt-sc-hr-invisible"></div>
<div class="dt-sc-hr-invisible-small"></div>
<div class="container">
<div class="dt-sc-portfolio-container">
<?php
$connection = mysql_connect("localhost", "root", "12345"); // Establishing Connection with Server
$db = mysql_select_db("icareimage");
//MySQL Query to read data
$query = mysql_query("SELECT photo FROM photos WHERE type='gallery'", $connection);
while ($row = mysql_fetch_array($query))
{?>
<div class="portfolio dt-sc-one-third column flexibility games">
<div class="portfolio-thumb">
<?php echo' <img src="images/photos/'.$row["photo"].'"/>'; ?>
<div class="image-overlay">
<div class="fig-content-wrapper">
<div class="fig-overlay">
<p>
</span>
</p>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</section>
</div>
</div>
<!-- support starts here -->
<!-- paralax starts here -->
<div class="fullwidth-section full-man">
<div class="container">
<div class="dt-sc-one-half column first animate" data-animation="fadeInUp" data-delay="100">
<h3 class="section-title">Make an Appointment</h3>
<div id="ajax_contact_msg"></div>
<form name="frmcontact" action="#" method="post" id="contact-form">
<div class="dt-sc-one-half column first">
<input type="text" name="txtname" placeholder="Enter name..." required>
</div>
<div class="dt-sc-one-half column">
<input type="email" name="txtemail" placeholder="Enter email..." required>
</div>
<div class="dt-sc-one-half column first">
<input type="text" name="txtname" placeholder="Enter Phone..." required>
</div>
<div class="dt-sc-one-half column">
<input type="email" name="txtemail" placeholder="Select Date..." required>
</div>
<div class="clear"></div>
<div class="selection-box">
<select name="cmbsubject">
<option value="Ask a Question?">Select Treatment</option>
<option value="">Cosmetic Dentistry</option>
<option value="">Dental Implants</option>
<option value="">Periodontal Care</option>
<option value="">Endodontic Treatment</option>
<option value="">Pediatric Dentistry</option>
<option value="">Orthodontics</option>
<option value="">Restorative Dentistry</option>
</select>
</div>
<div class="clear"></div>
<div class="selection-box">
<select name="cmbsubject">
<option value="Ask a Question?">What time you would like to come in</option>
<option value="">Morning</option>
<option value="">Afternoon</option>
<option value="">Evening</option>
</select>
</div>
<textarea name="txtmessage" placeholder="Type your queries..." required></textarea>
<input type="submit" name="submit" value="Submit Query">
</form>
</div>
<div class="dt-sc-one-half column"> <img src="images/man.png" alt="" title="" class="aligncenter"> </div>
</div>
</div>
<!-- paralax ends here -->
</section>
</div>
<!-- Team starts here -->
</div>
<!-- Team ends here -->
<!-- main-content ends here -->
</div>
<div class="dt-sc-hr-invisible-medium"></div>
<!-- footer starts here -->
<footer id="footer">
<div class="footer-widgets-wrapper">
<div class="container">
<div class="column dt-sc-one-fourth first">
<aside class="widget widget_text">
<div class="textwidget">
<h3 class="widgettitle"><span class="fa fa-user"></span>About Us</h3>
<p>The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, making it look like readable English. </p>
<p>The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters</p>
</div>
</aside>
</div>
<div class="column dt-sc-one-fourth">
<aside class="widget widget_text">
<h3 class="widgettitle"><span class="fa fa-link"></span>Ouick Links</h3>
<div class="textwidget">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Services</li>
<li>Appoinment</li>
<li>Galery</li>
<li>Contact Us</li>
</ul>
</div>
</aside>
</div>
<div class="column dt-sc-one-fourth">
<aside class="widget widget_text">
<h3 class="widgettitle"><span class="fa fa-link"></span>Our Services</h3>
<div class="textwidget">
<ul>
<li>Cosmetic Dentistry</li>
<li>Dental Implants</li>
<li>Periodontal Care</li>
<li>Endodontic Treatment</li>
<li>Pediatric Dentistry</li>
<li> Orthodontics </span> </span> </li>
<li> Restorative Dentistry </span> </span> </li>
</ul>
</div>
</aside>
</div>
<div class="column dt-sc-one-fourth">
<aside class="widget widget_recent_entries">
<h3 class="widgettitle"><span class="fa fa-calendar"></span>Contact Us</h3>
<!-- <div class="recent-posts-widget">
<ul>
<li> <img src="images/blog-thumb.jpg" alt="" title="">
<h4>Training with Dumbell</h4>
<div class="entry-metadata">
<p class="date">26 May 2014</p>
</div>
</li>
<li> <img src="images/blog-thumb1.jpg" alt="" title="">
<h4>Create the Adonis Effect</h4>
<div class="entry-metadata">
<p class="date">24 May 2014</p>
</div>
</li>
</ul>
</div>-->
<div class="textwidget">
<p>Image Dental clinic<br/>
Riyadh - Specialist Street<br/>
Tel : +966 1 4836060<br/>
Fax : +966 1 483770<br/>
</p>
</div>
</aside>
</div>
</div>
<div class="social-media-container">
<div class="social-media">
<div class="container">
<div class="dt-sc-contact-info dt-phone">
<p><i class="fa fa-phone"></i> <span>966 1 4836060</span> </p>
</div>
<ul class="dt-sc-social-icons">
<li class="facebook"></li>
<li class="google"></li>
<li class="twitter"></li>
<li class="youtube"></li>
<li class="rss"></li>
</ul>
</div>
</div>
</div>
</div>
<div class="copyright">
<div class="container">
<ul class="footer-links">
<li> About Us </li>
<li> Services </li>
<li> Contact Us </li>
</ul>
<p>© 2015 - Image Dental Clinic</p>
</div>
</div>
</footer>
</body>
</html>
//using image name
mysql_connect("localhost","xxxx","xxxxxxx");
mysql_select_db("wellho");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from im_library where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
//using image Id
include 'inc/db.php';
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT image FROM sites WHERE id = '$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/png");
echo $image;
$query = mysql_query("SELECT * FROM table_name WHERE id={$id}");
$result = mysql_fetch_array($query);
echo "<img src='images/$result["image_columnname"]' />";

echo $_SESSION broken

When I run the site, this dissapears on it:
<?php
echo $_SESSION["UserID"];
?>
I don't know why, it seems obvious it should work, it did in a video I watched? It kind of works when I make it:
<?php
echo '$_SESSION["UserID"]';
?>
But then it just echo's:
$_SESSION["UserID"]
And not the actual session id
Here is the whole script:
<?php require 'Connections/Connections.php'; ?>
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Titled Document</title>
<link href="C:/Users/Mikkel/Desktop/HTML & CSS/bootstrap.css" rel="stylesheet" />
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br><?php echo $_SESSION["UserID"]; ?>
<div id="Holder">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li>Login</li>
<li>Register</li>
<li>Forgot Password</li>
</ul>
</nav>
</div>
<div id="Content">
<div id="PageHeading">
<h1>Page Heading</h1>
</div>
<div id="ContentLeft">
</div>
<div id="ContentRight"></div>
</div>
<div id="Footer"></div>
</div>
</body>
</html>
And here is the script that makes it:
<?php require 'Connections/Connections.php'; ?>
<?php
if(isset($_POST['submit'])){
$UN = $_POST['username'];
$PW = $_POST['password'];
$result = $con->query("select * from user where Username='$UN' AND Password='$PW'");
$row = $result->fetch_array(MYSQLI_BOTH);
session_start();
$_SESSION["UserID"] = $row['UserID'];
header('Location: account.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="C:\Users\Mikkel\Desktop\HTML & CSS" rel="stylesheet" />
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />
<link href="CSS/Bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Holder">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li>Login</li>
<li>Register</li>
<li>Forgot Password</li>
</ul>
</nav>
</div>
<div id="Content">
<div id="PageHeading">
<h1>Page Heading</h1>
</div>
<div id="ContentLeft">
<h2>Your Message Here </h2><br />
<h6>Your Message</h6>
</div>
<div id="ContentRight">
<form id="form1" name="form1" method="post">
<div class="FormElement">
<p>
<input name="text" type="text" required="required" id="username" placeholder="Username">
</p>
<p> </p>
</div>
<div class="FormElement">
<p>
<input name="password" type="password" required="required" id="password" placeholder="Password">
</p>
<p> </p>
</div>
<div class="FormElement">
<p>
<input name="submit" type="submit" class="btn-primary" id="submit" value="Submit">
</p>
<p> </p>
</div>
</form>
</div>
</div>
<div id="Footer"></div>
</div>
</body>
</html>
It is usually best practices to start the session in the beginining of the document and not in the middle. However, your issue is that when you make the query, it does not bring in the variables $UN and $PW. Since you started it off with a " then, PHP will not parse the inside of it. You can "pause" it with: "select * from user where Username='" . $UN . "' AND Password='" . $PW . "'"
So, the session is never being properly set.
However, since the above method is unconventional, and since you are doing authentication, you should be using prepared statements. http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Categories