I created a signup/login system with PHP, and it works. Now I’m trying to have the index page display two images and two strings if the user is logged in. However, when I log in and go to the index page, it displays two broken image link icons and does not display either of the strings. I know I’m logged in, but I don’t know why the following does not result in two images and two strings being displayed.
I am still learning PHP syntax. I tried different variations of single quotes vs. double quotes, and I tried writing the session variables into the HTML tags directly rather than assigning them to $a, $b, etc.
<?php
session_start();
include_once 'includes/database_connection.inc.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/1.css">
</head>
<body>
<div id="div_name">
<img id="index_logo" src="images/logo.jpg">
<?php
if (isset($_SESSION['example_0'])) {
$a = "images/folder 1/folder 2/" . $_SESSION['example_1'] . ".png";
$b = "images/folder 1/folder 2/" . $_SESSION['example_2'] . ".png";
$c = $_SESSION['example_3'];
$d = $_SESSION['example_4'];
echo '<img id="id_example_a" src="<?php echo $a; ?>">';
echo '<img id="id_example_b" src="<?php echo $b; ?>">';
echo '<p id="id_example_c"><?php echo $c; ?></p>';
echo '<p id="id_example_d"><?php echo $d; ?></p>';
}
else {
echo '
<a href="login.php">
<button id="login_link_button">LOG IN</button>
</a>
';
echo '
<a href="signup.php">
<button id="signup_link_button">SIGN UP</button>
</a>
';
}
?>
2
Answers
You are echoing inside an echo
Assuming the session variables
example_1
andexample_2
contain valid and existing image names, then change to this – although I would style the link as a button instead of having a button in a linkYou’re using
echo
inside anecho
statement.You may wish to update these lines:
To:
You’ll notice I inverted your single and double quotes.
When you echo with double quotes, PHP will evaluate this and replace any variables you specify. The curly brackets help PHP know that these are variables. They are optional but best practice.
In Single quotes, variables will be output as such. For example: