Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Using PHP withing a IF/ELSE statement

    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 Code:
    <?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 Code:
    <?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!
    Last edited by ChetG; 03-23-2010 at 08:30 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The fact you're trying to jump into PHP mode when you're already in PHP mode may be something to do with it.

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

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

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

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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

    Edit:
    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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •