How to make an page only accessible when logged in, in PHP?
I want you when you click on the background to defend two buttons with Login And Register and you can only access these two buttons after you log in.
<?php
session_start();
include_once 'dbconnect.php';
?>
<!DOCTYPE html>
<HTML>
<head>
<title>NCS pagina principala</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" >
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" >
</head>
<body>
<?php if (isset($_SESSION['usr_id'])) { ?>
<FONT size="4%" color="#66ffff" FACE="cursive">Esti conectat cu:</FONT><i><FONT size="5" color="#66ff66"> <?php echo $_SESSION['usr_name']; ?></FONT></i>
<link rel="stylesheet" href="styles/buttonstyle.css">
<a class="button" href="logout.php">Delogheaza-te</a>
<?php } else { ?>
<link rel="stylesheet" href="styles/buttonstyle.css">
<a class="button" href="login.php">Logheaza-te</a>
<a class="button" href="form.html">Cerere cont</a>
<?php } ?>
<head>
<a class="button" href="blacklist.php">BlackList</a>
<CENTER>
<BODY STYLE = "BACKGROUND: url(https://cdn.discordapp.com/attachments/389773843813629972/389781868247253002/thumb-1920-553248.jpg); BACKGROUND-SIZE:130%"></BODY>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
</HTML>
try this (after session_start(); ):
if (!isset($_SESSION['usr_id'])) header("Location:logout.php");
and logout.php must be
<?php
session_start();
session_destroy();
header("Location:login.php");
?>
then, in login.php you write your login code
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
This form is my home page. After I log in I cant proceed to the home page
because of this error.
Parse error: syntax error, unexpected end of file in
C:\xampp\htdocs\kusina_online\home.php on line 28
<?php
include('session.php');
if(!isset($_SESSION['login_user'])) {
header("location: index.php");
?>
<DOCTYPE! html>
<html lang="en">
<head>
<title>Kusina Online</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial- scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="bootstrap/js/jquery-slim.min.js"></script>
<script type="text/javascript" src="bootstrap/js/popper.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;">
<h3 align="center">Kusina Online</h3>
<br />
<b id="welcome">Howdy, <i><?php echo $login_session; ?></i></b>
<div class="desc"><b id="logout">Log Out</b></div>
</div>
</body>
</html>
PHP is returning this error because you haven't properly ended your if statement. If you did your code would look like the following:
if(!isset($_SESSION['login_user'])) {
header("location: index.php");
}
Or if you only need to run one line of code you can format it like below
if(!isset($_SESSION['login_user']))
header("location: index.php");
you can refer it here.
<?php
include('session.php');
if(!isset($_SESSION['login_user'])) {
header("location: index.php");
} // you not closing this statement.
?>
<DOCTYPE! html>
<html lang="en">
<head>
<title>Kusina Online</title>
<meta charset="utf-8"> <!--- maybe a typo why would `/` be here.--->
<meta name="viewport" content="width=device-width, initial- scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="bootstrap/js/jquery-slim.min.js"></script>
<script type="text/javascript" src="bootstrap/js/popper.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;">
<h3 align="center">Kusina Online</h3>
<br />
<b id="welcome">Howdy, <i><?php echo $login_session; ?></i></b>
<div class="desc"><b id="logout">Log Out</b></div>
</div>
</body>
make sure to check every single line
you should put a closing bracket in line 6.
<?php
include('session.php');
if(!isset($_SESSION['login_user'])) {
header("location: index.php");
}
?>
You forgot closing the bracket in line 6 ...
<?php
include('session.php');
if(!isset($_SESSION['login_user'])) {
header("location: index.php");
?>
<DOCTYPE! html>
<html lang="en">
<head>
<title>Kusina Online</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial- scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="bootstrap/js/jquery-slim.min.js"></script>
<script type="text/javascript" src="bootstrap/js/popper.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:500px;">
<h3 align="center">Kusina Online</h3>
<br />
<b id="welcome">Howdy, <i><?php echo $login_session; ?></i></b>
<div class="desc"><b id="logout">Log Out</b></div>
</div>
</body>
</html>
I am currently using a template file called single-product.php and want to check for page template in header.php. I've tried using get_page_template_slug($post->ID) but it returns empty. I've also tried is_page_template('single-product.php'); but that returns nothing, as well.
Here's my header. You'll notice that I'm trying to get the template file through s_page_template('single-product.php') ny setting it as the value of $isProductPodTemplate at the top and use it in the if statement at the bottom:
<?php
$page = get_the_title($post->ID);
$frontpageId = get_option('page_on_front');
$isStandardTemplate = is_page_template('standard.php');
$isContactTemplate = is_page_template('contact.php');
$isProductPodTemplate = is_page_template('single-product.php');
?>
<!DOCTYPE html>
<html id="mainHTML" lang="en">
<head>
<title>Architectural Iron Works</title>
<meta name="description" content="Architectural Iron Works">
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel=icon type="img/ico" href=/favicon.ico> -->
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/main.bundle.css" />
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<?php wp_head(); ?>
<script type="text/javascript">
document.getElementById("mainHTML").style.display = "none";
</script>
</head>
<body>
<!-- Header -->
<?php
if ($post->ID == $frontpageId)
{
get_template_part('/includes/_home-header');
echo "<div class='main-content'>";
}
else if (is_404() ||
$isContactTemplate ||
$isStandardTemplate)
{
get_template_part('/includes/_no-banner-header');
echo "<div class='main-content no-banner'>";
}
else if ($isProductPodTemplate)
{
// do something
}
else
{
get_template_part('/includes/_default-header');
echo "<div class='main-content'>";
}
?>
When I query in MySQL then it returns in Japanese Character. An image is attached below.
But in browser its shown "????". An image is attached below.
Here I enclose my base.html code.
<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link media="screen" href="<?php echo $this->path_to('/common/css/import.css') ?>" type="text/css" rel="stylesheet" />
<script src="<?php echo $this->path_to('/common/js/jquery-2.1.0.min.js') ?>"></script>
<title><?php echo $this->v('page_title') ?></title>
</head>
<body>
<?php echo $this->part_of('body') ?>
</body>
</html>
Use mysqli_set_charset($db,'utf8'); in your dbconnection file. in my case $db is my connection variable
I have a problem with including a php file
PagePrevie.php :
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="Style/css/bootstrap.min.css" />
<script src="Style/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="Style/css/Header-Picture.css">
<link rel="stylesheet" href="Style/css/Footer-with-logo.css">
<title></title>
</head>
<body class="container lg-warning">
<?php
include 'include/LogoUMBB.php';
include 'include/Header.php';
?>
<div class="container bg-success text-primary" style="width:500px;padding: 20px;">
</div>
<br />
<?php
include 'include/Footer.php';
?>
</body>
</html>
The problem is that this file is located in a directory that contains other web page with the same code
And here is the structure of files:
PHP told you your path is incorrect, add "../" so you get correct include path.
include '../include/LogoUMBB.php';
include '../include/Header.php';
include '../include/Footer.php';
I am trying to get echo $userRow['student_firstname']; to pass into the html so I can use it as a automatic way to change displaying the users first name & logout with register & login. The first echo $userRow['student_firstname']; does work, however the second does not. The idea would be able to get the second to work, so I can remove the first. If there isn't a session then it displays the login & register.
<?php
session_start();
include_once 'dbconnect_new.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
<link href="favicon.png" type="image/x-icon" rel="shortcut icon">
<link href="assets/css/master.css" rel="stylesheet">
<script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
</head>
<?php if(isset($_SESSION['user'])) {
// error_reporting(E_ALL ^ E_DEPRECATED);
$res=mysql_query("SELECT * FROM studentdata WHERE student_id=".$_SESSION['user']);
if ($res === FALSE){
die(mysql_error());
}
while($userRow=mysql_fetch_array($res))
{
echo $userRow['student_firstname'];
}
?>
<li class="dropdown">
<a href="">
<?php echo $userRow['student_firstname'];?><span class="nav-subtitle">Account</span></a</li>
<li>Logout<span class="nav-subtitle">Goodbye</span></li>
<?php } else { ?>
<li>Register<span class="nav-subtitle">for Students</span></li>
<li>Login<span class="nav-subtitle">for Students</span></li>
<?php } ?>
</html>
Check this link out http://php.net/manual/en/function.mysql-fetch-array.php,
Tip: USE mysqli/pdo as mysql is deprecated.
<?php
session_start();
include_once 'dbconnect_new.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
<link href="favicon.png" type="image/x-icon" rel="shortcut icon">
<link href="assets/css/master.css" rel="stylesheet">
<script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
</head>
<?php
if (isset($_SESSION['user'])) {
// error_reporting(E_ALL ^ E_DEPRECATED);
$res = mysql_query("SELECT * FROM studentdata WHERE student_id=" . $_SESSION['user']);
if ($res === FALSE) {//IF QUERY RETURNS NULL
die(mysql_error());
} else {
$userRow = mysql_fetch_array($res, MYSQL_ASSOC); //DATA IN ARRAY TYPE
//IF BELOW DOES NOT WORK USE print_r(); to check the structure of array !
?>
<li class="dropdown">
<?php echo $userRow['student_firstname']; //ACCESS DATA THROUGH ITS INDEX ?><span class="nav-subtitle">Account</span>
</li>
<li>
Logout<span class="nav-subtitle">Goodbye</span>
</li>
<?php
}
} else {
?>
<li>Register<span class = "nav-subtitle">for Students</span></li>
<li>Login<span class = "nav-subtitle">for Students</span></li>
<?php } ?>
</html>
I Hope this works !