PHP button link (header) not responding - php

I have created a button start_btn that is supposed link me to an internal header, but it is not responding at all.
It shows up, but even though I implemented other actions that it's supposed to take it, doesn't work.
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
//////
if (isset($_POST['start_btn'])) {
session_destroy();
unset($_SESSION['username']);
header('location: register.php');
}
//////
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<!-- logged in user information -->
<?php if (isset($_SESSION['username'])) : ?>
<p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
<div class = "btn">
<button type="submit" class="btn" name="start_btn">Start!</button>
</div>
<p> logout </p>
<?php endif ?>
</div>
</body>
</html>
I have created working login and register buttons and tried to implement their code into my new button, but it still doesn't work.

Related

two php files simultaneously

I was trying to make a website. So this is the index.php page.
When 'more info' of any of the form is clicked, the user is redirected to a payment.php page, where the user must make the payment. Once the payment is done, the user is redirected to success.php page, which is supposed to show these 3 lines for two seconds and then redirect the user to details.php page. However, for some reason, instead of redirecting to details.php, both details.php and index.php come up simultaneously like this. How can I avoid the index file from being there too? I just want to show the details file.
Here is the code of the success page:
<?php
include 'index.php';
if(!empty($_GET['tid'] && !empty($_GET['product']))) {
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
$tid = $GET['tid'];
$product = $GET['product'];
} else {
header('Location: payment.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Thank You</title>
</head>
<body>
<div class="container mt-4">
<h2>Thank you for purchasing <?php echo $product; ?></h2>
<hr>
<p>Your transaction ID is <?php echo $tid; ?></p>
<p>Check your email for more info</p>
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
</div>
</body>
</html>
I feel that this is the most important part of the success.php code:
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
here's the details page:
<?php
include 'config/db_connect.php';
include 'config/db.php';
include 'index.php';
if (isset($_POST['delete'])) {
$id_to_delete = mysqli_real_escape_string($conn, $_POST['id_to_delete']);
$sql = "DELETE FROM customers WHERE id = $id_to_delete";
if (mysqli_query($conn, $sql)) {
header('Location: index.php');
} else {
echo 'query error: ' . mysqli_error($conn);
}
}
// check GET request id param
if (isset($_GET['id'])) {
// escape sql chars
$id = mysqli_real_escape_string($conn, $_GET['id']);
// make sql
$sql = "SELECT * FROM customers WHERE id = $id";
// get the query result
$result = mysqli_query($conn, $sql);
// fetch result in array format
$customer = mysqli_fetch_assoc($result);
mysqli_free_result($result);
//mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<?php include 'templates/header.php'; ?>
<div class="container center grey-text">
<?php if ($customer) : ?>
<h4><?php echo $customer['Job_Type']; ?></h4>
<p>Contact Number of loan enquirer: <?php echo $customer['Telephone']; ?></p>
<p>Annual income: <?php echo 12 * $customer['Monthly_salary']; ?></p>
<p>Existing loan amount: <?php echo $customer['Existing_loan_amount']; ?></p>
<p>Residential_Type: <?php echo $customer['Residential_Type']; ?></p>
<p>Job: <?php echo $customer['Job']; ?></p>
<p>Form submission time: <?php echo date($customer['Form_Submission_Time']); ?></p>
<!-- DELETE FORM -->
<form action="details.php" method="POST">
<input type="hidden" name="id_to_delete" value="<?php echo $customer['id']; ?>">
<input type="submit" name="delete" value="Delete" class="btn brand z-depth-0">
</form>
<?php else : ?>
<h5>No such customer exists.</h5>
<?php endif ?>
</div>
<?php include 'templates/footer.php'; ?>
</html>
Your details page starts with these three line:
include 'config/db_connect.php';
include 'config/db.php';
include 'index.php';
As you can see, in the third line, you include index.php. My best guess is that that is the reason you see it in the details page.

Parse error: syntax error, unexpected end of file in php on line 48 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
afternoon\am having difficulties in determining the error of the code and i would need assistance. each time i execute the code am given an error in which i am unable to determine the error itself
<?php
include('functions.php');
if (!isLoggedIn()) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<!-- logged in user information -->
<div class="profile_info">
<img src="images/user_profile.png" >
<div>
<?php if (isset($_SESSION['user'])) : ?>
<strong><?php echo $_SESSION['user']['username']; ?></strong>
<small>
<i style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i>
<br>
logout
</small>
<?php endif ?>
</div>
</div>
</div>
</body>
</html>
You are missing a closing bracket in the first lines of your code :
<?php
include('functions.php');
if (!isLoggedIn()) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
} // <- Here is the mistake
?>

Redirecting HomePage

Iam Learning PHP, so i have started an website..in that i have created index.php and index_1.php and login.php in which when user successful login the user has to be redirect to index.php but iam not getting it so i have created an another index_1.php in this page only three navigation bars will be there Home,projects and contactus.
This is my index.php Page
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Login</li>
<li>Register</li>
<li>Contact</li>
</ul>
</div>
</div>
This is my index_1.php
<?
session_start();
if(!isset($_SESSION['user_email']))
{
echo '<p>Please Login to continue Log In</p>';
exit();
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<li>Contact</li>
<?php
echo '<p align="right">';
session_start();
echo "Welcome";
echo '<br>';
echo ($_SESSION ['user_email']);
echo '<br> Logout?ClickHere</p>';
?>
This is my login.php
<?php
ob_start();
session_start();
if(isset($_POST["Submit"]))
{
$user_email=$_POST['user_email'];
$user_password=md5($_POST['user_password']);
$con=#mysql_connect('localhost','xxxxx','xxxx') or die(mysql_error());
mysql_select_db('suryapra_aenugula_karthik');
$query=mysql_query("SELECT * FROM user_registration where user_email='".$user_email."' AND user_password='".$user_password."'") or die("error in selection");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['user_email'];
$dbpassword=$row['user_password'];
}
if($user_email==$dbusername && $user_password==$dbpassword)
{
if(isset($_POST['remember']))
{
setcookie('user_email',$user_email,time()+60*60*7);
setcookie('user_password',$user_email,time()+60*60*7);
}
session_start();
$_SESSION['user_email']=$user_email;
header("Location: index_1.php");
ob_end_flush();
}
}
else
{
header("Location: login_2.php");
ob_end_flush();
}
}
else
{
header("Location: login.php");
}
?>
what my problem is if user logins he is redirecting to index.php and again login and register links are also getting in index.php
i want solution in which when user logins he should be redirect to index.php and login,register tabs should not be visible to him unless he presses logout button
The basic flow of User Authentication:
User submits login form to login.php this, if login is correct, will set a $_SESSION variable to signal user is logged in.
User is then redirected back to site, where
In the templates, anything that is optional for loggedin/logged out users are wrapped in IF blocks
Something like so:
<?php
$logged_in = $_SESSION['logged_in'];
?>
<nav>
<?php if (!$logged_in):?>
Login
<?php endif;?>
<?php if ($logged_in):?>
Logout
<?php endif;?>
</nav>
First of all after a successful login on the login page redirect user on the index.php page.
you dont need index_2.php page.
After that edit your index page with following code where first we will check if [user_email] session is been created or not when we logged in if it is created we are not echoing anything if not then we will echo the login and register link. bellow is your new index.php page
<?php session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1>KarthikAenugula</h1>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Home</li>
<li>Projects</li>
<li>AboutMe</li>
<?php if(isset($_SESSION[user_email]))
{
}
else
{
echo "<li><a href='login.php'>Login</a></li>";
echo "<li><a href='register.php'>Register</a></li>";
}
?>
<li>Contact</li>
</ul>
</div>
</div>

Admin and Customer login in Php

I have some code which attempts to check whether a user is an admin or customer to login, and then there's one problem I can't solved it. When I use the customer user's account to login, for example using redhood as my customer's username to access the customer login page. But when I use the admin user's account to login, for example using wolfpack as my admin's username, it redirect me to the customer login page instead of the admin login page. But I don't know which php code I need to change. Can anyone solve this problem? Thanks!
MySql Database:
Signin.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/signin/signin_desktop.css">
<link rel="stylesheet" type="text/css" href="css/signin/signin_tablet.css" media="screen and (max-width:768px)">
<link rel="stylesheet" type="text/css" href="css/signin/signin_mobile.css" media="screen and (max-width:480px)">
</head>
<body>
<div id="wrapper">
<header>
<nav id="mainMenu">
<img src="logo/logo.png" id="logo">
<ul>
<li>Home</li>
<li>About</li>
<li>Booking Rates</li>
<li>Recreation</li>
</ul>
</nav>
</header>
<section id="banner">
<div id="ritu" class="shadow">
<img src="images/content_pure.jpg">
<img src="images/Sports-Hub-Gym.jpg">
<img src="images/gym.jpg">
<img src="images/ClubFitt2jpg.jpg">
</div>
</section>
<section id="content">
<div id="sign">
<div class="user">
<h2>Existing User</h2>
<form action="checkuser.php" method="post">
<p style="color:white;">Username:</p>
<input type="text" name="uname" size="25" maxlength="20" placeholder="Please enter your username" required><br><br>
<p style="color:white;">Password:</p>
<input type="password" name="pword" size="25" maxlength="20" placeholder="Please enter your password" pattern=".{6,}" required><br><br>
<input type="submit" name="loginbtn" value="">
</form>
<?php
if(isset($_GET['uname']) && isset($_GET['pword']))
{
echo "<script>alert('Invalid Username and Password.')</script>";
}
?>
</div>
<div class="user">
<h2>New User</h2>
<img id="create" src="button/CREATE%20ACCOUNT.png">
<p style="color:white;">Ads:</p>
<div id="ads">
<div class="row">
<div class="image">
<img id="minilogo" src="logo/logo.png">
<div id="advt">
Download Now
</div>
</div>
<div class="image">
<img id="apps" src="images/myActiveSG%20APP.jpg">
</div>
</div>
</div>
</div>
</div>
</section>
<footer>
<p>© Copyright 2016 SportLab. All Rights Reserved.</p>
<nav id="submenu">
<ul>
<li>Sitemap | </li>
<li> Contact |</li>
<li>Term of Use </li>
<li>| Privacy </li>
</ul>
</nav>
</footer>
</div>
</body>
</html>
PHP
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Check Login</title>
</head>
<body>
<?php
if(isset($_POST["loginbtn"]))
{
$u=$_POST['uname'];
$p=$_POST['pword'];
$conn=mysqli_connect("localhost", "root", "" , "M3_156020K_Syahri_SportFacility");
$sql = "SELECT * FROM create_user WHERE username='" .$u. "' AND password='" .$p. "' ";
$search_result=mysqli_query($conn, $sql);
$userfound=mysqli_num_rows($search_result);
if($userfound >= 1)
{
session_start();
$_SESSION['MM_Username']= $u;
$row=mysql_fetch_assoc($search_result);
if($row['role'] == 1)
{
header("Location: login_admin.html");
}
else
{
header("Location: login_cust.html");
}
}
else
{
header("Location: signin.html?uname=" . $u . "&pword=" . $p);
}
mysqli_close($conn);
}
?>
At a quick glance, one of the things you should correct, is to not send output until AFTER you have finished working with the session. Also, as Rafael mentioned, you are checking if the field equals 1, and not the actual value in the field.
For example, moving the html block to after your conditions like so ( Updated this example to use prepared statements ) :
<?php
if(isset($_POST["loginbtn"])) {
$u=$_POST['uname'];
$p=$_POST['pword'];
$conn=new mysqli("localhost", "root", "" , "M3_156020K_Syahri_SportFacility");
$sql = "SELECT `role` FROM create_user WHERE username=? AND password=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $u, $p);
$stmt->execute();
$stmt->bind_result($role);
$stmt->close();
$conn->close();
if($role) {
session_start();
$_SESSION['MM_Username']= $u;
if($role == 'Admin') {
header("Location: login_admin.html");
} else {
header("Location: login_cust.html");
}
} else {
header("Location: signin.html?uname=" . $u . "&pword=" . $p);
}
mysqli_close($conn);
}
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Check Login</title>
</head>
<body>
At the end of the day, I would seriously reconsider writing your own login system from scratch especially given the apparent lack of attention to obvious security holes. I would recommend a package out of the box such as otp-thing or if you must write the whole portal yourself, something like Laravel/etc.
Aside from the security issues in the code others have stated, to answer your question...your condition is wrong:
if($row['role'] == 1) should be changed to if ($row['role'] == "Admin")
In your case the role is never 1 and therefore will always hit the else condition sending them to login_cust.html.
Your database roles are only User and Admin. Change the condition.

Php after redirect the button doesn't work

I have two PHP pages:
giardino.php:
<?php
SESSION_START();
if (isset($_SESSION['utente'])){
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="immagini/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<link rel="stylesheet" href="css/stile.css"/>
</head>
<body>
<div data-role="page" data-theme="d">
<div data-role="header">
Ambienti
<a href="logout.php" data-role="button" data-theme="b" data-icon="delete" >Logout</a>
<h1>Domos - Giardino</h1>
</div><!-- /header -->
<div data-role="content" id="centramento">
<?php
include("configurazione.php");
$id_stanza=1;
$pin=26;
//lettura stato attuale
$comando="select luci from casa where ID_stanza=1";
$query=mysql_query($comando,$connessione);
while($riga=mysql_fetch_array($query)){
$oldstate=$riga['luci'];
}
if($oldstate == 0){
$newstate='accendi luce';
$theme='e';
}
else{
$newstate='spegni luce';
$theme='a';
}
echo "<a href='luce.php' data-role='button' data-theme='$theme' id='radio'>$newstate</a>"
?>
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<h4>Credits: Silvio Mattiello 5C Informatica 2014/2015</h4>
</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>
<?php
}
else{
header("location:home.php?msg=6");
}
?>
luce.php:
<?php
$id_stanza=1;
$pin=$_GET=26;
$comando = escapeshellcmd("sudo /var/www/domotica/python/luce.py $pin $id_stanza");
$esito = exec($comando);
if ($esito == "allarme attivato"){
header("location:ambienti.php");
exit();
}
else {
header("location:giardino.php");
exit();
}
?>
When I click the button on the first page, I get redirected to the second page.
In the second page, there are some operations and then I get redirected to the first page (or another page, ambienti.php).
But when I am redirected to the first page again, the button on this page (giardino.php) doesn't work. I can't click it. Why?
try to use a require instead of the include, to see if there is any error... It seems to be something with include.
Also, you could try to add 302 http to the header like this and add the full path
header('Location: http://server_name/ambienti.php', true, 302);

Categories