skip to Main Content

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


  1. You are echoing inside an echo

    Assuming the session variables example_1 and example_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 link

    <!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'];
        ?>
        <img id="id_example_a" src="<?php echo $a; ?>">
        <img id="id_example_b" src="<?php echo $b; ?>">
        <p id="id_example_c"><?php echo $c; ?></p>
        <p id="id_example_d"><?php echo $d; ?></p>
    <? } 
      else { ?>
         <a href="login.php">
            <button id="login_link_button">LOG IN</button>
          </a>
          <a href="signup.php">
            <button id="signup_link_button">SIGN UP</button>
          </a>
    <? } ?>
    
    Login or Signup to reply.
  2. You’re using echo inside an echo statement.

    You may wish to update these lines:

        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>';
    

    To:

        echo "<img id='id_example_a' src='{$a}'>";
        echo "<img id='id_example_b' src='{$b}'>";                    
        echo "<p id='id_example_c'>{$c}</p>";
        echo "<p id='id_example_d'>{$d}</p>";
    

    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:

    $var = 'Hello World!';
    
    echo '$var'; // Output: $var
    
    // Both of the below output: Hello World!
    echo "$var";
    echo "{$var}";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search