Unexpected T_STRING with PHP session variables [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
I have been attempting to create a user login script in PHP, however I am met with an error telling that $_SESSION["username"] is undefined. I have session_start() inside of $_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.header.php", however the variable still shows as undefined.
<?php
if (!isset($_SESSION["username"])) {
header("Location: /en/");
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.header.php");
?>
<title>Profile | Project Club</title>
</head>
<body>
<h1>Profile</h1>
<p><?php echo $_SESSION["username"]?></p>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.footer.php");
?>
</body>
</html>

Use session_start() at the beginning of the code.

Related

I can't figure out what's wrong with this code [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
When I run this code, it always shows:
Parse error: syntax error, unexpected end of file in /opt/lampp/htdocs/japan_top10_09112018/index.php on line 30
<?php
echo "<title>Top 10 Things to Do in Japan</title>";
require_once('templates/header.php');
?>
<?php
// Ask teacher: does this code is right to show errors or necessary?
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
?>
<?php
require_once('database/data.php');
?>
<div class = "container">
<h1>Top 10 things to do in Japan</h1>
<?php
foreach($data as $info) {
print_r($info["Picture"]);
echo "<hr>";
// echo '<img src="$info["Picture"]" class="img-thumbnail">';
?>
<?php
require_once('templates/footer.php');
?>
The line 30 is the last line.
You are forgetting to add a } to close your foreach

How do you change a CSS link document with PHP if/else? Displays no error message [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
Trying to set up templates on a site I am working on, and I want to change the linked stylesheet document instead of using PHP within the CSS. Would this be possible? Thanks.
<?php
require 'includes/templates.php';
if($_COOKIE['template'] == $blocky) {
echo "<link rel="stylesheet" href="/testpages/css/blocky.css">";
}
else {
echo "<link rel="stylesheet" href="/testpages/css/default.css">";
}
?>
Yes that would possible, but make sure to escape your quotes
<?php
require 'includes/templates.php';
if($_COOKIE['template'] == $blocky) {
echo "<link rel=\"stylesheet\" href=\"/testpages/css/blocky.css\">";
}
else {
echo "<link rel=\"stylesheet\" href=\"/testpages/css/default.css\">";
}
?>

php include file stopped working [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
How do I get PHP errors to display?
(27 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 5 years ago.
Learning PHP and MySQL. Made a short script that retrieves contents from the database, and populates it in a tag in my html header.
index.php:
<?php
$link = mysqli_connect('localhost', 'root', 'abdullah');
if (!$link) {
echo "Could not connect";
exit();
}
if (!mysqli_select_db($link, 'chitra')){
echo "Could not find database";
exit();
}
$query = 'SELECT * FROM photos;';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$categories[] = $row['category'];
}
include 'main.html';
?>
main.html:
<!DOCTYPE html>
<html>
<head>
<title>Chitra</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<header>
<div id="header_username">username</div>
<div id="header_cat">
<select>
<?php foreach ($categories as $category): ?>
<option><?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?></option>
<?php endforeach; ?>
</select>
</div>
</header>
<main>
TODO
</main>
</body>
</html>
This code was initially working. I started working on the body for a while, until suddenly, it started returning me a blank white page. I removed my main content to start over from scratch, but it still give me the same result.
I put echo statemnts throughout the PHP file, everything is executing fine until right before the include statement. I checked my Apache, MySQL, PHP installations, checked that everything is up and running. Everything is. I can't figure out the issue.
You can not use raw PHP functions etc in HTML files,
try to change the extension to .php maybe this helps you.

I can't get to echo images with php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to echo multiple images but i get the error:
"PHP Parse error: syntax error, unexpected '?>' in
/public_html/View/Pages/Home.php on line 17".
Can you give me any guidance on how to fix it? code below
<?php
for($i=0;$i<$length;$i++) {
echo ?><img class="meme-image" src="<?php $meme["$imd_id" == "$i"]->$path?>"><?php
}
?>
Try By this.
<?php
for($i=0;$i<$length;$i++) {
echo '<img class="meme-image" src='.$meme[$imd_id == $i]->$path.' ">';
}
?>
Note : You do not require to close php tag. instead of that insert this in php echo.and also you must read this For best practice.

PHP include() gets error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have 35.php that includes common menu for my website. 36.php page should display the menu by using include()
Why doesn't this work?
My error:
35.php is shown below;
<?php
echo "Home
PHP
HTML
CSS <br />";
?>
36.php is shown below;
enter code here
<html>
<body>
<?php include("35.php"); ?>
<p>About Menu is imported from 35.php file</p>
</body>
</html>
You missused the ". They include the string but you also used them for the href. But that closes the string
echo "<a href="
This is where the String ends for PHP and it wants either a ; fo finish the statement or a . to connect it to another string.
So this is what you could to to solve it:
echo 'Home
PHP
HTML
CSS <br />';
With switching the "to a ' The string symbol is now the ' and therefor the " can be used int he string normaly.
Otherwise you could also escape the " like this \" inside the string:
echo "Home ...

Categories