Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What am I doing wrong with these PHP session variables?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
<? } ?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading