...

View Full Version : Resolved Using PHP withing a IF/ELSE statement



ChetG
03-23-2010, 08:12 PM
Hello all!

I am working on a new site for a client, and I'm trying something I've never tried before. I am using Wordpress to make their life easier, and I'm using its is_user_logged_in() function.

Basically this is what I want to do:



<?php
if ( is_user_logged_in() ) {
Display Private Info;
}
else {
Display Log-In Form
};
?>


I am focusing on getting the log-in form working, and I can't figure out how to get more PHP to run in the else statement. It keeps throwing unexpected < errors.

Here is the problem piece:



<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
<?php
$blogurl = bloginfo('url');
$postlink = $_GET['redirect'];
?>

<link rel="stylesheet" href="<?=$blogurl ?>/wp-admin/css/login.css" type="text/css" />

<div id="login">

<form name="loginform" id="loginform" action="<?=$blogurl ?>/wp-login.php" method="post">
<p>
<label>Username<br />
<input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10" /></label>
</p>
<p>
<label>Password<br />
<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>

</p>
<p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> Remember Me</label></p>
<p class="submit">
<input type="submit" name="wp-submit" id="wp-submit" value="Log In" tabindex="100" />
<input type="hidden" name="redirect_to" value="<?=$postlink ?>#respond" />
<input type="hidden" name="testcookie" value="1" />
</p>
</form>

<p id="nav">
<a href="<?=$blogurl ?>/wp-login.php?action=lostpassword" title="Password Lost and Found">Lost your password?</a>
</p>

</div>

<p id="backtoblog"><a href="#" class="lbAction" rel="deactivate">Close</a></p>

<script type="text/javascript">
try{document.getElementById('user_login').focus();}catch(e){}
</script>
};
?>


I hope one of you geniuses out there are able to help out. Thanks in advance for all of your advice!

MattF
03-23-2010, 08:31 PM
The fact you're trying to jump into PHP mode when you're already in PHP mode may be something to do with it.



} else {
<?php
$blogurl = bloginfo('url');
$postlink = $_GET['redirect'];
?>


Change to:



} else {
$blogurl = bloginfo('url');
$postlink = $_GET['redirect'];
?>



Edit: Ditch the short tag syntax in your echo's too.

Fou-Lu
03-23-2010, 08:33 PM
You should always post the error to make it clearer and easier to find. In this case, its a parse error on line 5, you cannot have the <?php within the actual PHP code block. Just remove that <?php immediately following the else {, and you should be good.

This had been previously marked as resolved; I assume you figured what the problem was? Good to always post back / make an edit indicating if you've solved it - a few threads went forever since they were marked as resolved when they weren't hah O.o


Ack, matt snuck in there O.o
Oh, I think it was either TomWS or Fumigator mentioning that the wordpress does this all over their code. Weird standard too, seems to be line for line O.o
I thought that to be odd :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum