954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Variable hyperlink using if statement with PHP

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';
  }
?>
GreaseJunkie
Light Poster
33 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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>";
}
fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

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; ?>"/>
GreaseJunkie
Light Poster
33 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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?

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

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

fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

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

GreaseJunkie
Light Poster
33 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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.

GreaseJunkie
Light Poster
33 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: