I'm working on an if else statement that will display a custom link for everyone except Company A. Everything below works just fine, but it's imperative Company A not see the first link.

I've tried something along the line of:

<?php
$nolink = "Company A";
if ($_SESSION['company'] != $nolink);{ //display the link...etc
?>

...but the problem is, my custom link has nested php scripts. I think I'm on the right track, but I'm stuck. Again, my code below works except that I want the first link to disappear for Company A.

<h1>Welcome, 
<?php //display company name inside the h1 tag
if (!session_register(company));{
echo $_SESSION['company'];}
?>
</h1>

<p>
<!--DISPLAY THIS LINK FOR EVERYONE EXCEPT COMPANY A-->
<span style="margin-left: 50px;">
<a href="<?php  echo "folderName/" . $_SESSION['company'] . "/" ?>">&raquo; Go To <?php echo $_SESSION['company'];?> Account Info</a>
</span><br />

<!--DISPLAY THESE LINKS FOR EVERYONE-->
<span style="margin-left: 50px;">
<a href="link2.php">&raquo; Go To Link 2</a>
</span><br />
<span style="margin-left: 50px;">
<a href="link3.php">&raquo; Go To Link 3</a>
</span>
 </p>

Dani AI

Generated

Quick summary: the behavior you describe usually comes from a logic/typo issue and not from “nested PHP” itself. In ’s snippet a stray semicolon after the if effectively ended the condition so the block always ran; ’s post also relies on the old session_register approach (deprecated). Use a proper session start, read $_SESSION safely, compare the company name, and build the anchor in one PHP block so you don’t have to juggle nested open/close tags.

A compact, modern pattern (illustrates the safe checks and escaping to avoid XSS and URL problems):

<?php
session_start();

$company = isset($_SESSION['company']) ? trim($_SESSION['company']) : '';

if ($company !== '' && strcasecmp($company, 'Company A') !== 0) {
    $url   = 'folderName/' . rawurlencode($company) . '/';
    $label = htmlspecialchars($company, ENT_QUOTES, 'UTF-8');
    echo '<span style="margin-left:50px;"><a href="' . $url . '">&raquo; Go To ' . $label . ' Account Info</a></span><br />';
}
?>

Troubleshooting checklist:

  • Ensure session_start() runs before any output.
  • Remove accidental semicolons immediately after if(...). That single character turns the condition into a no-op.
  • Use isset()/trim() to avoid notices and stray whitespace.
  • Use rawurlencode() for URL parts and htmlspecialchars() for visible labels.
  • For case-insensitive matching use strcasecmp(); use strict operators (===/!==) when appropriate.
  • While testing, temporarily var_dump($_SESSION) to confirm the value.

As a final note: building the whole anchor in PHP is the simplest fix here; HEREDOC or template approaches work too, but avoid session_register and the semicolon trap.

Member Avatar for Member #380484

Sometimes it's challenging to not mix up your html tags and php code, but as much as you can manage them as separate logical pieces, the easier it is to make it all work ... in my opinion.

Something like this might work -- though this is off the top of my head and untested

<?php
// I may have messed this up some but it looked ... odd to me
if ( ! session_register('company') ) {
  $company = $_SESSION['company'];
}
// make your link  here
$link = <<<ENDLINK
    <span style="margin-left: 50px;">
      <a href="folderName/$company/">&raquo; Go To $company Account Info</a>
    </span><br />
ENDLINK;
// match $company to Company A and go from there
$output = ( $company == 'Company A' ) ? $link : '';
?>
<html>
<head>
</head>
<body>
  <h1>Welcome, </h1>
  <p>
    <!--DISPLAY THIS LINK FOR EVERYONE EXCEPT COMPANY A-->
<?php print $output ?>
    <!--DISPLAY THESE LINKS FOR EVERYONE-->
    <span style="margin-left: 50px;">
      <a href="link2.php">&raquo; Go To Link 2</a>
    </span><br />
    <span style="margin-left: 50px;">
      <a href="link3.php">&raquo; Go To Link 3</a>
    </span>
  </p>
</body>
</html>

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.