Hi everyone, thanks for reading this. I am trying to write some code so that a vote button won't allow a person to vote unless they are registered and logged in. I have this written, but it's not outputting properly. I'm pretty sure I am not encoding the URLS properly within the PHP tags. I can get different buttons/images to display based upon the logged-in status, but I need the URL to change also so they will be thanked for voting or be asked to register after clicking. Make sense? I've googled the crap out of this - I'm probably not using the correct phrasing with my search terms - I'm sure this has been done before.

Any help is appreciated! Thanks!

<?php
	ob_start();
    session_start();
    if(!isset($_SESSION['Username']))
    {
	$link = ( '<a href="http://www.test.com/signup.php"><img src=/images/register.png" /></a>' );
	$alt = 'Please Register';
	
}	else
	{
	$link = ( '<a href="http://www.test.com/thanks.php"><img src=/images/vote.png" /</a>' );
	$alt = 'Vote';
  }
?>

Recommended Answers

All 6 Replies

You have an error on line 11, you need to add ">" to then end of the image tag.
Also try this.

ob_start();    
session_start();    
if(!isset($_SESSION['Username'])) {	
    echo "<a href='http://www.test.com/signup.php'><img src='/images/register.png' alt='Please Register'/></a>";	
} else {	
    echo "<a href='http://www.test.com/thanks.php'><img src='/images/vote.png' alt='Vote'></a>";
}

Thank you for your help, but how do I get the string "$link" incorporated in there? I need this $link to replace the button hrefs

<img src="<?php $link; ?>"/>

What I can see is that the variable "link" contains both the href and the image itself.

<?php
	ob_start();
    session_start();
    if(!isset($_SESSION['Username']))
    {
        $alt = 'Please Register';
	$link = ( '<a href="http://www.test.com/signup.php"><img src=/images/register.png" alt="' .$alt. '" /></a>' );
        echo $link;
    }else{
        $alt = 'Vote';
	$link = ( '<a href="http://www.test.com/thanks.php"><img src=/images/vote.png" alt="' .$alt. '" /></a>' );
        echo $link;
	
  }
?>

Is that what you mean?

Ok, if you wrote a session login page, then you know something about variables! If you just want the href, then use it in a variable?

if logged in
$link = "http://www.test.com/signup.php";
$img = "/images/rester.png";
$alt = "Please Register";
else
$link = "http://www.test.com/thanks.php";
$img = "/images/vote.png";
$alt = "Vote";

Hope this helps

That way is super clean too! Thanks to all and to all a good night! (Merry Christmas reference)

So this is how the problem was solved for anyone interested:

Declare your variables within the 'if' statement. If they are false, the output would result in one URL; if true, a different URL and different voting button image. (In this case, if the user was logged in or not.) The "alt" is for the Alternate Text you must provide for any image placement, so we'd want this different if the image was different, obviously.

<?php
	ob_start();
    session_start();
    if(!isset($_SESSION['Username']))
    {
	$alt = 'Please Register';
	$link = 'http://www.test.com/signup.php';
	$img = '/images/register_but.png';
} else {
	$alt = 'Vote!';
	$link = 'http://www.test.com/thanks.php';
	$img = '/images/vote_but.png';
}
?>

And the calls for the variables would look like this where your voting buttons would be (or variable URLS and respective images).

<a href="<?php echo $link; ?>"><img src="<?php echo $img; ?>" alt="<?php echo $alt; ?>" />

Thanks again to fobos and phorce for your help. I over thought this one as usual. Keep It Simple Stupid are words to code by.

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.