How to change pages without changing url? - php

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>

Related

Sending Data from database in HTML email using PHP

i'm having trouble with my code, i want it to send a user their username via email when they have forgotten it.
What it does:
Retrieves user information from database
Send HTML Email to User's email
What it doesn't do:
Display the user's login username when sent to the email.
The PHP code is below for the page "forgot-username.php"
<?php
require_once('../../Connections/localhost.php');
require('../../PHPMailer/PHPMailerAutoload.php');
if(isset($_POST) & !empty($_POST)){
$username = mysql_real_escape_string($_POST['username'], $localhost);
$sql = "SELECT * FROM database.users WHERE users.email = '$email'";
$res = mysql_query($sql, $localhost) or die(mysql_error());
$count = mysql_num_rows($res);
if($count == 1){
$r = mysql_fetch_assoc($res);
$username = $r['username'];
$to = $r['email'];
$subject = "Your Recovered Username";
$message = file_get_contents("email_template.html");
$headers = 'From: Your name <info#address.com>';
if(mail($to,$subject,$message,$headers)){
header('Location: username-sent.html');
}else{
echo "Failed to Recover your email, try again";
}
}else{
echo "email does not exist in database";
}
}
?>
<!DOCTYPE html>
<!--[if IE 8 ]><html class="no-js oldie ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html class="no-js" lang="en">
<!--<![endif]-->
<head>
<!-- ====== Basic Page Needs ====== -->
<meta charset="utf-8">
<title>Forgot Username</title>
<!-- ====== Mobile Specs Meta ====== -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- ====== CSS ====== -->
<link rel="stylesheet" href="../../css/base.css">
<link rel="stylesheet" href="../../css/vendor.css">
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/zerogrid.css">
<!-- ====== Java Scripts ====== -->
<script src="../../js/modernizr.js"></script>
<script src="../../js/pace.min.js"></script>
<!-- ====== Favicon ====== -->
<link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
<link rel="icon" href="../../favicon.ico" type="image/x-icon">
</head>
<body id="top">
<!-- ====== Header Begin ====== -->
<header>
<div class="header-logo">
Logo
</div>
<a id="header-menu-trigger" href="#0">
<span class="header-menu-text" style="color: white">Menu</span>
<span class="header-menu-icon"></span>
</a>
<nav id="menu-nav-wrap">
<span>Close</span>
<img alt="KBG" src="../../images/logo.png" style="height: 100px;width: 100px;" />
<ul class="nav-list">
<li class="current"><a class="" href="../../index.html">Home</a></li>
<li><a class="" href="../../about.html" title="">About</a></li>
<li>
<a class="" >Services</a>
<div>
<ul>
<li>Service 1</li>
<li>Service 2</li>
<li>Service 3</li>
<li>Service 4</li>
</ul>
</div>
</li>
<li><a class="" href="../../Service5.html" >Service 5</a></li>
<li><a class="" href="../../competition.html" >Competition</a></li>
<li><a class="" href="../../login.php" >Sign In</a></li>
<li><a class="" href="../../signup.html" >Sign Up</a></li>
<li>Contact Us</li>
</ul>
</nav> <!-- end #menu-nav-wrap -->
</header>
<section id="services">
<div class="overlay"></div>
<div class="zerogrid">
<div class="row">
<!--Start Box-->
<div class="col-1-3 offset-1-3">
<div class="wrap-col">
<div class="row">
<div class="animate-this">
<h1 style="color: white">Forgot username?</h1>
<p class="lead" style="color: white">Fill in your email below and we will send you your username.</p>
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<form method="post">
<div class="form-field">
<input name="email" style="color: white" class="full-width" type="text" id="email" placeholder="Email" value="" minlength="5" required>
</div>
<div class="form-field">
<input type="submit" class="submitform full-width" style="background-color: white; color: black" value="Send" name="forgotusername">
</div>
Go Back
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<footer>
<div class="footer-bottom">
<div class="row">
<div class="col-full">
<div class="copyright">
<span>© Copyright Company 2017.</span>
</div>
</div>
</div>
</div>
<div id="go-top">
<a class="smoothscroll" title="Back to Top" href="#top">
<i class="fa fa-long-arrow-up" aria-hidden="true"></i>
</a>
</div>
</footer>
<div id="preloader">
<div id="loader"></div>
</div>
<!-- ====== Java Scripts ====== -->
<script src="../../js/jquery-2.1.3.min.js"></script>
<script src="../../js/plugins.js"></script>
<script src="../../js/main.js"></script>
<script src="../../js/cookiechoices.js"></script>
<script>
cookieChoices.showCookieBar({
linkHref: '../../cookie-policy.html',
language: 'en'
});
</script>
</body>
</html>
this is the code that is sent to the users email "email_template.html"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="icon" href="https://www.website.com/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="https://www.website.com/css/base.css">
<link rel="stylesheet" href="https://www.website.com/css/vendor.css">
<link rel="stylesheet" href="https://www.website.com/css/main.css">
<link rel="stylesheet" href="https://www.website.com/css/zerogrid.css">
</head>
<body style="max-width: 100%">
<section style="background-color: black; text-align: center; height: 20%; min-height: 5%"><img src="https://www.website.com/images/logo.png" alt="logo" style="max-height: 150px; max-width: 150px; margin-top: 1%" /></section>
<section id="services">
<div class="overlay"></div>
<div class="zerogrid">
<div class="row">
<!--Start Box-->
<div class="col-1-3 offset-1-3">
<div class="wrap-col">
<div class="row">
<div class="animate-this">
<h1 style="color: white">Forgot Your Username?</h1>
<p class="lead" style="color: white">Not a problem, we've searched our database for your username.</p>
<p class="lead" style="color: white">Your username is:</p>
<div class="form-field">
<input type="button" class="full-width" style="background-color: white; color: black" value="$username">
</div>
<br />
<br />
<p class="lead" style="color: white">Regards,<br />Support Team</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
Everything does as it should but the username doesn't show up, instead "$username" shows up in the email.
Someone please help. Thanks in advance.
First of all, email_template.html is an .html file, you need to change it to .php file.
Second, you are getting the file contents to variable how would you expect $username variable to be in same scope ?
Probable solution would be :
Get the file content and then replace the $username in your .html file content with $username value.
$message = file_get_contents("email_template.html");
$message = str_replace('$username', $username, $message);
file_get_contents() function does not parse the contents. SO you have to add this line after you get contents into $message :
$message = str_replace('$username', $username, $message);
And I suggest that you replace $username in your html by something like %USERNAME% and you use it like that :
$message = str_replace('%USERNAME%', $username, $message);
You can't use PHP code inside a HTML document. Any PHP code in HTML document will parse as a string.
Change file extension for 'email_template.html' to. Php:
email_template.php
Note that now you will have to use PHP echo to print any HTML. I suggest to read how php works: http://www.php.net

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">

Using sessions to display a users name on website

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; ?>

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>

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