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">
Related
I have a header file which has the navigation menu in it which I include on every page using function (<?php include_once 'header.php'; ?> ). But weirdly some things like my "search" button on pages other then home (index) look differently (I mean header.css dosent work with some elements)!
I will attach screenshots of the home and signup page below so you can see what I mean.
Another "header" releated issue happens when I add tag on the index page (to create side menu) it looks like the css of the "header" although "header" has its own css and index has another one.
Can someone tell me why this is happening! Apparently I cant add images here yet but if you click on teh files below it will open up and you will see what I mean. Thanks
Index page
<?php
include_once 'header.php';
?>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="static/index.css">
</head>
<body>
<div class="category-tree">
<h4 class="cat-title">Categories</h4>
<nav>
<ul class="left-menu-categories">
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
</ul>
</nav>
</div>
</body>
Header file
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>PHP Project</title>
<link rel="stylesheet" type="text/css" href="static/header.css">
</head>
<body>
<nav>
<img src="static/img/logo.png" href="index.php" style="width: 100px">
<div class="wrapper">
<ul>
<div class="search-bar">
<form>
<input class="search-box" type="text" name="" placeholder="Search...">
<button type="submit" class="button">Search</button>
</form>
</div>
<?php
if (isset($_SESSION["userid"])) {
echo "<li><button class='sell' href=''>Button</button></li>";
echo "<li><a href='profile.php'>My Profile</a></li>";
echo "<li><a href='includes/logout.inc.php'>Log out</a></li>";
}
else {
echo "<li><a href='signup.php'>Sign Up</a></li>";
echo "<li><a href='login.php'>Login</a></li>";
}
?>
</ul>
</div>
</nav>
</body>
<div class="wrapper">
Home page
Home page
Login page
Login page
PHP is a serversided language. Include parses a file server sided into another file befor it is sended to users browser for rendering.
So your document would look like:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>PHP Project</title>
<link rel="stylesheet" type="text/css" href="static/header.css">
</head>
<body>
<nav>
<img src="static/img/logo.png" href="index.php" style="width: 100px">
<div class="wrapper">
<ul>
<div class="search-bar">
<form>
<input class="search-box" type="text" name="" placeholder="Search...">
<button type="submit" class="button">Search</button>
</form>
</div>
<?php
if (isset($_SESSION["userid"])) {
echo "<li><button class='sell' href=''>Button</button></li>";
echo "<li><a href='profile.php'>My Profile</a></li>";
echo "<li><a href='includes/logout.inc.php'>Log out</a></li>";
}
else {
echo "<li><a href='signup.php'>Sign Up</a></li>";
echo "<li><a href='login.php'>Login</a></li>";
}
?>
</ul>
</div>
</nav>
</body>
<div class="wrapper">
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="static/index.css">
</head>
<body>
<div class="category-tree">
<h4 class="cat-title">Categories</h4>
<nav>
<ul class="left-menu-categories">
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
</ul>
</nav>
</div>
</body>
to which you surely agree would be an invalid HTML markup. You have multipel <head> and <body> sections.
Your actual files should look like this:
Index.php:
<?php
include_once 'header.php';
?>
<div class="category-tree">
<h4 class="cat-title">Categories</h4>
<nav>
<ul class="left-menu-categories">
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
<li class="cat-1">Title</li>
</ul>
</nav>
</div>
</body>
</html>
header.php:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>PHP Project</title>
<link rel="stylesheet" type="text/css" href="static/header.css">
</head>
<body>
<nav>
<img src="static/img/logo.png" href="index.php" style="width: 100px">
<div class="wrapper">
<ul>
<div class="search-bar">
<form>
<input class="search-box" type="text" name="" placeholder="Search...">
<button type="submit" class="button">Search</button>
</form>
</div>
<?php
if (isset($_SESSION["userid"])) {
echo "<li><button class='sell' href=''>Button</button></li>";
echo "<li><a href='profile.php'>My Profile</a></li>";
echo "<li><a href='includes/logout.inc.php'>Log out</a></li>";
}
else {
echo "<li><a href='signup.php'>Sign Up</a></li>";
echo "<li><a href='login.php'>Login</a></li>";
}
?>
</ul>
</div>
</nav>
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>
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; ?>
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>
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