How to keep session variables between pages with forms? - php

I'm writing a program that has 3 pages.
On page 1 there is an option for the user to select a quantity of a breakfast product he wants to purchase. After selecting a quantity the user hits the submit button, if the user is not registered, it will take him to Page 2 for him to register. If the user is registered, it will direct them to Page 3.
However, if the user goes to Page 2 first and does not have a quantity selected from Page 1 it will redirect him to Page 1 after he registers and press submit, and then once they select a quantity and hit submit on Page 1 it will go to Page 3.
I'm struggling to maintain my session variables between the pages because two of them have forms that get overwritten if the user ever goes back to that page.
Page 1:
<?php
session_start();
$_SESSION['name']= $_POST['name'];
$_SESSION['email']= $_POST['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Product Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a class="active" href="product.php">Product</a></li>
<li>Registration</li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<?php
$action = '';
if (!empty($_SESSION['name']) or !empty($_SESSION['email'])) {
$action = "invoice.php";
}
else {
$action = "registration.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
<div class="container">
<img src="images/platter.jpg" alt="Breakfast Platter" style="float: left; width: 400px; height: 300px;";>
<h1>Breakfast Platter</p>
<p>The breakfast platter option comes with two fried eggs, four pancakes, and a bunch of bacon.</p>
Quantity: <input type="number" name="platter_quantity" min="0">
<p value="10.99" name="platter_price">Price: $10.99</p>
</div>
<div class="container">
<img src="images/yogurt.jpg" alt="Yogurt Parfait" style="float: left; width: 400px; height: 300px;">
<h1>Yogurt Parfait</p>
<p>The yogurt parfait option comes with two cups of yogurt, oats, and a mixture of berries.</p>
Quantity: <input type="number" name="yogurt_quantity" min="0">
<p value="6.99" name="yogurt_price">Price: $6.99</p>
</div>
<div class="container">
<img src="images/waffles.jpg" alt="Waffles" style="float: left; width: 400px; height: 300px;";>
<h1>Waffles</p>
<p>The waffles option comes with two buttermilk waffles with butter and syrup.</p>
Quantity: <input type="number" name="waffles_quantity" min="0">
<p value="$4.99" name="waffles_price">Price: $4.99</p>
</div>
<br>
<button class="button" type="submit" name="submit">Submit</button>
</form>
</body>
</html>
Page 2:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$_SESSION['platter_quantity'] = $_POST['platter_quantity'];
$_SESSION['yogurt_quantity'] = $_POST['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $_POST['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li><a class="active" href="registration.php">Registration</a></li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<br>
<?php
$action = '';
if (!empty($_SESSION['platter_quantity']) or !empty($_SESSION['yogurt_quantity']) or !empty($_SESSION['waffles_quantity'])) {
$action = "invoice.php";
}
else {
$action = "product.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
Name: <input type="text" name="name" pattern="[A-Za-z]" required><br><br>
E-mail: <input type="text" name="email" pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required><br><br>
<input type="submit">
</form>
<br>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $_SESSION["platter_quantity"] . "<br>";
echo "Yogurt: " . $_SESSION["yogurt_quantity"] . "<br>";
echo "Waffles: " . $_SESSION["waffles_quantity"];
?>
</body>
</html>
Page 3:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Invoice Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li>Registration</li>
<li><a class="active" href="invoice.php">Invoice</a></li>
<li style="float:right">Login</li>
</ul>
<h1>Hi! Welcome <?php echo $_SESSION['name']; ?>! </h1>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $platter_quantity . "<br>";
echo "Yogurt: " . $yogurt_quantity . "<br>";
echo "Waffles: " . $waffles_quantity;
?>
</body>
</html>
What's the best way for me to implement this using session variables without using a database?
I tried doing this as well, but it did not seem to work:
<?php
session_start();
if (empty($_SESSION['name']) or empty($_SESSION['email'])) {
$_SESSION['name'] = $POST_['name'];
$_SESSION['email'] = $POST_['email'];
}
else {
$name = $_SESSION['name'];
$name = $_SESSION['email'];
}
if (empty($_SESSION['platter_quantity']) or empty($_SESSION['yogurt_quantity']) or ($_SESSION['waffles_quantity'])) {
$_SESSION['platter_quantity'] = $POST_['platter_quantity'];
$_SESSION['yogurt_quantity'] = $POST_['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $POST_['waffles_quantity'];
}
else {
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
}
?>

You never insert $_POST or $_GET without first checking if they're set isset($_POST['variable']), and you can use checks here as well - do a check for the existence of $_POST-variables, and if they exist, use them, and if not, assign the $_SESSION-variables. So on page 3, you will have something like:
$name = $_SESSION['name'] = (isset($_POST['name']) ? $_POST['name'] : ((isset($_SESSION['name']) ? $_SESSION['name'] : '')));
And so on for the other variables. What this does is checks for $_POST, and if it's set, it updates the $_SESSION-variable, and if it's not set, it just updates the $_SESSION-variable with the already existing $_SESSION-variable, and if that doesn't exist either, it sets both variables $name and $_SESSION['name'] to empty, which you then can check for later in the script (and redirect etc.)

Related

how to create similar php pages but with different urls

I'm currently learning php so I'm a beginner I have learned the basics and some advanced stuff but now I'm trying to make a project to help me learn faster which will be basically a Math test in times table in which a user will enter the site, and then the user will enter his name and click 'Begin test' to enter the test which is 10 questions. the user need to answer the question first by clicking on a button and then click on 'Next' to go to next question and after finishing 10 questions the result will be shown to him something like "You have answered CorrectAnswersNumber from total of 10 questions!".
I have made something like this when I was learning ASP.Net MVC but in php it is a bit complicated. So my question is should I need to create 10 php pages that contains code to generate random numbers for the questions? if so how can I pass whether the user has answered the question right or wrong?
What I have did so far is the page in the first which contain the username and save it by using a session here is my code for the index.php page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD">
<h1 style="font-size: 75px;" align="center">Math Test</h1>
<form method="POST" action="Math_Test.php">
<div style="text-align: center;">
<input type="text" name="Name" style="width: 500px; height: 100px; font-
size: 75px; color: blue;">
<br /><br />
<input type="submit" value="Begin Test" style="width: 250px auto;
height: 100px auto; font-size: 75px;">
</div>
</form>
</body>
</html>
and the code for the secondpage:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$Name = $_POST['Name'];
} else {
echo "<h1 align='center' style='margin-top: 250px;'>Sorry, You can't
access this page directly.<br /> Please go back ant try again or simply
click here!</h1>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD;">
<?php echo "<h1 align='center' style='font-size: 100px;'>Hi " . $Name .
"</h1>"; ?>
</body>
</html>
You can do something like this on your index page
<?php
// starts session
session_start();
// check if submit button was clicked
if ( isset($_POST['submit']) ) {
$name = htmlspecialchars(trim($_POST['name']));
// check if name was provided
if ( $name !== '' ) {
// store name in session
$_SESSION = array(
'answered_questions' => 0,
'correct_answers' => 0,
'wrong_answers' => 0,
'name' => $name
);
// redirect to second page
header('Location: second_page.php');
exit();
}
// set error if name was not provided
$error = 'Please provide your name';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body style="background-color: #DDD">
<h1 style="font-size: 75px;" align="center">Math Test</h1>
<?php
// no name was provided so display the error message
if ( isset($error) ) {
echo '<p style="color: red">'. $error . '</p>'
}
?>
<form method="POST" action="">
<div style="text-align: center;">
<input type="text" name="name" style="width: 500px; height: 100px; font-
size: 75px; color: blue;">
<br /><br />
<input type="submit" name="submit" value="Begin Test" style="width: 250px auto;
height: 100px auto; font-size: 75px;">
</div>
</form>
</body>
</html>
And then on your second page you do something like so
<?php
// starts session
session_start();
// check if name key and value is not in the session
// if not there, then redirect back to the index.php page
if ( !isset($_SESSION['name']) ) {
header('Location: index.php');
exit();
}
if ( isset($_POST['submit']) ) {
$answer = (int)$_POST['answer'];
$expected_answer = $_SESSION['first'] * $_SESSION['second'];
$_SESSION['answered_questions'] += 1;
if ( $expected_answer == $answer ) {
$_SESSION['correct_answers'] += 1;
} else {
$_SESSION['wrong_answers'] += 1;
}
if ( $_SESSION['answered_questions'] == 10 ) {
// reset the session values from that page except name of course
header('Location: show_results.php');
exit();
}
$_SESSION['first'] = rand(1, 10); // random number between 1 and 20
$_SESSION['second'] = rand(1, 10);
} else {
$_SESSION['first'] = rand(1, 10); // random number between 1 and 20
$_SESSION['second'] = rand(1, 10);
}
// set the variable name to value in the session
$name = $_SESSION['name'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math Test</title>
</head>
<body>
<?php var_dump($_SESSION); ?>
<h1>Hello, <?php echo $name; ?></h1>
<form method="POST" action="">
<p><?php
echo $_SESSION['first'] . ' * ' . $_SESSION['second'] . ' = ';
?><input type="text" name="answer" id="answer" required></p>
<p><input type="submit" name="submit"></p>
</form>
</body>
</html>
Then after if you want you can handle all the logic on the second page if you want as per #SamiKuhmonen advice
You don't need to do 10 pages for PHP to handle the math test that you are trying to do, instead, there is a technique called AJAX, which you can read about it from this URL:
https://www.tutorialspoint.com/php/php_and_ajax.htm
they have a very nice tutorial that deals with databases as well, which will help you in your case.
Give it a look and try to implement it, it will help you learn much faster.
Just for the record, PHP does have MVC frameworks, two are popular for developers,
Laravel: https://laravel.com/
Codeigniter: https://codeigniter.com/
Both supports MVC, I'll recommend you to go first with Codeigniter, once you feel that you are comfortable in PHP, switch to Laravel.
All the best, happy coding!

i am trying to learn php and following a tutorial from youtube $_SESSION is driving me crazy ..i tried everything to make it work

i have declared a session_start() function in the start of both the pages but still the variable is not passing on to the session variable please help
this is where i have included my login.php
<?php
include("template/header.php");
include("template/content.php");
include("template/footer.php");
include("login.php");
?>
this my login.php file where i have passed $email variable to SESSION
<?php
session_start();
include("includes/connection.php");
if(isset($_POST['login'])){
$email= mysqli_real_escape_string($con,$_POST['email']);
$pass= mysqli_real_escape_string($con,$_POST['pass']);
$select_user = "select * from users where user_email= '$email' AND
user_pass='$pass' AND status='verified'";
$query = mysqli_query($con,$select_user);
$check_user= mysqli_num_rows($query);
if($check_user===1){
$_SESSION['usermail']=$email;
echo "<script>window.open('home.php','_self')</script>";
} else {
echo "<script>alert('incorrect details try again')</script>";
}
}
?>
and this is where i have tried to access the session variable but it says undefined:usermail but i dont understand i am giving the session_start() at the beginning and have checked that $email is successfully getting its value from the database then why this is not working
<?php
session_start();
include("includes/connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome User!</title>
<link rel="stylesheet" href="styles/home_style.css" media="all"/>
</head>
<body>
<!--container starts-->
<div class="container">
<!--header wrapper starts here-->
<div id="head_wrap">
<!--header starts-->
<div id="header">
<ul id="menu">
<li>Home</li>
<li>Members</li>
<strong>Topics:</strong>
<?php
$get_topics = "select * from topics";
$run_topics= mysqli_query($con,$get_topics);
while($row=mysqli_fetch_array($run_topics)){
$topic_id = $row['topic_id'];
$topic_title = $row['topic_name'];
echo "<li><a href='topic.php?
topic=$topic_id'>$topic_title</a></li>";
}
?>
</ul>
<form method="get" action="results.php" id="form1">
<input type="text" name="user_query" placeholder="search a
topic"/>
<input type="submit" name="search" value="search"/>
</form>
</div><!--header ends-->
</div><!--head wrap ends-->
<!--content area starts-->
<div class="content">
<!--user timeline starts here-->
<div id="user_timeline">
<div id="user_details">
<?php
$user=$_SESSION['usermail'];
var_dump($_SESSION);
$get_user="select * from users where user_email='$user'";
$run_user= mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_id= $row['user_id'];
$user_name= $row['user_name'];
$user_country= $row['user_country'];
$user_image= $row['user_image'];
$register_date= $row['user_reg_date'];
$last_login= $row['user_last_login'];
$user_posts="select * from posts where user_id='$user_id'";
$run_posts = mysqli_query($con,$user_posts);
$posts =mysqli_num_rows($run_posts);
//getting the number of unread messages
$sel_msg = "select * from messages where receiver='$user_id' AND
status='unread' ORDER by 1 DESC";
$run_msg = mysqli_query($con,$sel_msg);
$count_msg = mysqli_num_rows($run_msg);
echo "
<center>
<img src='users/default.png' width='200' height='200'?>
</center>
<dev id='user_mention'>
<p><strong>Country:</strong>$user_country</p>
<p><strong>Last Login:</strong>$last_login</p>
<p><strong>Member Since:</strong>$register_date</p>
<p><a href='my_messages.php?inbox&u_id=$user_id'>Messages
($count_msg)</a></p>
<p><a href='edit_profile.php?u_id=$user_id'>Edit my account</a>
</p>
<p><a href='logout.php'>Logout</a></p>
</div>
";
?>
</div><!--user details ends here-->
</div><!--user timeline ends here-->
</div><!--content area ends-->
</div><!--container ends-->
</body>
</html>

Duplicating instead of updating

Hello guys
I have a db with guides that in admin mode can be edited. I have just remade the input area and all is good except when logged in as admin i cant update guides, it simply creates a new guide instead of simply updating.
Please be gentle with me as i am a beginner in the coding world, + i would love some fresh eyes on this :) thank you very much
my dashboard code
<?php include("header.php"); ?>
<?php
if(!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"){
die("<script> window.location = 'login.php' </script>");
}
$error=false;
$success=false;
if(isset($_GET) && !empty($_GET)) {
$id = base64_decode($_GET['id']);
$user_id = $_SESSION['userInfo']['id'];
$selectSql = "SELECT * FROM guides WHERE 1 = 1 AND user_id = " . $user_id . " AND id = " . $id;
$result = $conn->query($selectSql);
$id = 0;
$title = $step2 = $step3 = $step4 = $step5 = $step6 = $step7 = $step8 = $step9 = '';
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$title = $row['title'];
$step2 = $row['step2'];
$step3 = $row['step3'];
$step4 = $row['step4'];
$step5 = $row['step5'];
$step6 = $row['step6'];
$step7 = $row['step7'];
$step8 = $row['step8'];
$step9 = $row['step9'];
}
}
}
if(isset($_POST) && !empty($_POST)){
$user_id = $_SESSION['userInfo']['id'];
if($_POST['id']){
$sqlInsert = 'UPDATE guides SET title = "'.htmlentities($_POST["title"]).'", step2 = "'.htmlentities($_POST["step2"]).'", step3 = "'.htmlentities($_POST["step3"]).'", step4 = "'.htmlentities($_POST["step4"]).'", step5 = "'.htmlentities($_POST["step5"]).'", step6 = "'.htmlentities($_POST["step6"]).'", step7 = "'.htmlentities($_POST["step7"]).'", step8 = "'.htmlentities($_POST["step8"]).'", step9 = "'.htmlentities($_POST["step9"]).'" WHERE id = ' . $_POST['id'] . ' AND user_id = ' . $_SESSION['userInfo']['id'];
}else{
$sqlInsert = 'INSERT INTO guides(user_id, title, step2, step3, step4, step5, step6, step7, step8, step9)VALUES ("' .$user_id. '", "'.htmlentities($_POST["title"]).'", "'.htmlentities($_POST["step2"]).'", "'.htmlentities($_POST["step3"]).'", "'.htmlentities($_POST["step4"]).'", "'.htmlentities($_POST["step5"]).'", "'.htmlentities($_POST["step6"]).'", "'.htmlentities($_POST["step7"]).'", "'.htmlentities($_POST["step8"]).'", "'.htmlentities($_POST["step9"]).'")';
}
if ($conn->query($sqlInsert) === TRUE) {
if($_POST['id']){
$success = "Your guide has been updated successfully!";
}else{
$success = "Your guide has been added successfully!";
}
$_SESSION['success'] = $success;
header("Location: dashboard.php");
}else{
$error[] = "Error Message: ".$conn->error;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Guideory - share your knowledge</title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-
scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Guideory - share your knowledge" />
<!--web-fonts-->
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<!--web-fonts-->
</head>
<body>
<div class="header">
</div>
<!---header--->
<!---main--->
<div class="main">
<div class="main-section">
<div class="login-form">
<h2>Share a piece of your knowledge</h2>
<br>
<h4>You can create up to 8 steps, not including the title.
Atleast one step is required. When writing your guide, remember that other
people have to be able to read it, so be as specific as possible.</h4>
<form role="form" method="post">
<div id="step-1">
<ul>
<li class="text-info" id="title">Title:</li>
<li><input type="text" value="<?php echo $title;
?>" name="title" id="title" placeholder="Enter the title for your guide here"
required></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info" id="step2">Step 1:</li>
<li><textarea name="step2" id="step2"
placeholder="Enter the description for step 1 here" required><?php echo
$step2; ?></textarea></li>
<div class="clear"></div>
</ul>
<br>
<ul>
<li class="text-info">Step 2:</li>
<li><textarea name="step3" placeholder="Enter the
description for step 2 here"><?php echo $step3; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 3:</li>
<li><textarea name="step4" placeholder="Enter the
description for step 3 here"><?php echo $step4; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 4:</li>
<li><textarea name="step5" placeholder="Enter the
description for step 4 here"><?php echo $step5; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 5:</li>
<li><textarea name="step6" placeholder="Enter the
description for step 5 here"><?php echo $step6; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 6:</li>
<li><textarea name="step7" placeholder="Enter the
description for step 6 here"><?php echo $step7; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 7:</li>
<li><textarea name="step8" placeholder="Enter the
description for step 7 here"><?php echo $step8; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 8:</li>
<li><textarea name="step9" placeholder="Enter the
description for step 8 here"><?php echo $step9; ?></textarea></li>
<div class="clear"></div>
</ul>
<input type="submit" value="Create guide">
</form>
</div>
</div>
</div>
</body>
</html>
There is no input with name id so $_POST['id'] doesn't exist and that's why there's an insert instead of update.
And some extra hints
!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"
You probably want isset($_SESSION['isLogin']) here since when the variable is not set it can never be YES
isset($_GET) && !empty($_GET)
You can drop isset here and only use empty.
while ($row = $result->fetch_assoc()) {
Only the last row is stored in those variables, since you are overwriting them.

How to set a cookie for sign up and login script

I am having problem in setting cookies. I have a sign up and login form from where after users are redirected to another page which includes header.php which is to show different data to logged in users and guest visiters(not logged in). But after sign up and login header menus are still sign up and login
register.php
<?php
$con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established");
?>
<?php
if(isset($_POST['reg'])){
$fn=strip_tags(#$_POST['fname']);
$ln=strip_tags(#$_POST['lname']);
$un=strip_tags(#$_POST['username']);
$em=strip_tags(#$_POST['email']);
$pswd=strip_tags(#$_POST['password']);
$d= date("Y-m-d");
$reg_query = "INSERT INTO users (userid,username,first_name,last_name,email,password,sign_up_date,activated,bio,profile_photo,closed) VALUES ('','{$un}','{$fn}','{$ln}','{$em}','{$pswd}','{$d}','0','What you do?','','no')";
$reg_run = mysqli_query($con,$reg_query);
setcookie('user',$un,time()+3600*24*365);
echo "The cookie has been set.";
header("Location:main.php");
if(isset($_POST['login'])){
$log_que = "SELECT * FROM users WHERE username = '$un' AND password = '$pswd' ";
$log_run = mysqli_query($con, $log_que);
$row = mysqli_fetch_array($log_run);
$un_db = $log_row['username'];
$pswd_db = $log_row['password'];
if($un == $un_db && $pswd == $pswd_db)
{
echo "LOGGED IN!";
setcookie('user',$un,time()+3600*24*365);
header("Location: main.php");
}
}
?>
header.php
<!DOCTYPE html>
<html>
<head>
<title>findfriends</title>
<script src="js/main.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="./img/logo.gif"/>
</div>
<div class="search_box">
<form action="searchresults.php" method="post" name="search">
<table>
<tr>
<td>
<input type="text" name="search" placeholder="Search ..."/>
</td>
<td>
<input type="image" src="./img/search-icon.png" alt="submit" />
</td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_COOKIE['user'])){
echo '
<ul class="dd">
<li><a href="main.php" >Home</a>
</li>
<li>Profile
</li>
<li>Inbox' . $unread_numrows . '
</li>
<li>Management
<ul><li>Settings
</li>
<li>Logout
</li>
</ul>
</li>
</ul>';
}
else
{
echo '
<ul class="dd">
<li><a href="register.php" >Sign Up</a>
</li>
<li>Login
</li>
</ul>';
}
?>
</div>
</div>
</body>
</html>
You need to set output buffer on , if you do output before header function
Add it at your first line
<?php ob_start(); ?>
And at last
<?php ob_flush(); ?>
you must add an if else statement in your login.php / login.html file
every file of your page
if($user===not_logged_in){
header("Location: login.php");
} else{
//do somthing else
}

Add additional Rows in PHP

I need to add a button to add more rows, maybe 10 at a time? I also need each added row to have it's own variable name right? So it will echo out in the email template below? Or can it just be a bunch of fields to echo out? I have a team of non-programmers that will be using this form to print out the email template below it, so they can copy and paste. Would really be nice if it could have a button to copy to clipboard, but thats not the main issue.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove Bad Links Template</title>
<style>
body {font-family: 'Hammersmith One', sans-serif; color:#333; }
#wrapper { width:75%; margin:0 auto; background:#f2f2f2; border:#ccc; border-radius: 10px; box-shadow:3px 3px 3px #ccc; padding:20px; }
#emailback { background: #fff; padding:20px; }
h1, h2 { color:#0074b9; }
</style>
<link href='http://fonts.googleapis.com/css?family=Hammersmith+One' rel='stylesheet' type='text/css'>
</head>
<body>
<?php
$name = $_GET['name'];
$link1 = $_GET['link1'];
$link2 = $_GET['link2'];
$link3 = $_GET['link3'];
$link4 = $_GET['link4'];
$link5 = $_GET['link5'];
$link6 = $_GET['link6'];
$link7 = $_GET['link7'];
$link5 = $_GET['link8'];
$link6 = $_GET['link9'];
$link7 = $_GET['link10'];
?>
<div id="wrapper">
<h1>Use the following template to request bad link removal</h1>
<form action="" method="GET">
<h3>WEBMASTERS NAME: <input type="text" name="name"></h3>
<h3>BAD LINK 1 <input type="text" name="link1"></h3>
<h3>BAD LINK 2 <input type="text" name="link2"></h3>
<h3>BAD LINK 3 <input type="text" name="link3"></h3>
<h3>BAD LINK 4 <input type="text" name="link4"></h3>
<h3>BAD LINK 5 <input type="text" name="link5"></h3>
<h3>BAD LINK 6 <input type="text" name="link6"></h3>
<h3>BAD LINK 7 <input type="text" name="link7"></h3>
<h3>BAD LINK 8 <input type="text" name="link8"></h3>
<h3>BAD LINK 9 <input type="text" name="link9"></h3>
<h3>BAD LINK 10 <input type="text" name="link10"></h3>
<input type="submit">
</form>
<p>
<h2>THEN COPY AND PASTE THE FOLLOWING AFTER SUBMITTING THE ABOVE FORM: </h2>
<div id="emailback">
Hi <?php echo $name; ?>, </p>
<p>
We have recently received a notification from Google stating that our website has unnatural links pointing towards it. This has really damaged our rankings on Google and as a result, we're trying to clear things up. Our website url is http://********.com.
</p><p>
We noticed the following links are pointing to our website from your site:
</p><p>
<h3><?php echo $link1; ?></h3>
<h3><?php echo $link2; ?></h3>
<h3><?php echo $link3; ?></h3>
<h3><?php echo $link4; ?></h3>
<h3><?php echo $link5; ?></h3>
<h3><?php echo $link6; ?></h3>
<h3><?php echo $link7; ?></h3>
<h3><?php echo $link8; ?></h3>
<h3><?php echo $link9; ?></h3>
<h3><?php echo $link10; ?></h3>
</p><p>
I appreciate this is inconvenient and isn't a reflection on your website at all, but if you're able to remove the links, we would really appreciate it and would be very grateful.
</p><p>
I look forward to hearing from you.
</p><p>
Jerry *****
</p><p>
*********
</p>
</p>
</div>
</div>
</body>
</html>
Alright, let's start with some DRY work.
<?php
$name = isset($_GET['name']) ? $_GET['name'] : 'Webmaster';
$links = $_GET['links'] ? $_GET['links'] : array_fill(0, 10, '');
if (isset($_GET['add-rows'])) {
for ($i = 0; $i < 10; $i++) {
$links[] = '';
}
}
?>
And then modify your form:
<form method="GET">
<h3>WEBMASTERS NAME: <input type="text" name="name"></h3>
<?php
for ($i = 0; $i < count($links); $i++) {
echo '<h3>BAD LINK ' . ($i + 1) . ' <input type="text" name="links[]"></h3>';
}
?>
<input name="add-rows" type="submit" value="Add Rows">
<input type="submit" value="Submit">
</form>
And finally, change how your links output:
<p>
<?php
for ($i = 0; $i < count($links); $i++) {
echo '<h3>' . $links[$i] . '</h3>';
}
?>
</p>

Categories