Jump to content

HTML Form Inside PHP IF Statement


JellyFishBoy

Recommended Posts

Hey guys!

 

Im trying to setup an IF statement on my home page.

 

Its purpose is to show EITHER a login form and state your not logged in if it cannot find a stored cookie OR if it does find a stored cookie it will NOT show the HTML form and state you are logged in.

 

I have it mostly working apart from the fact Im not sure how to insert a HTML form into PHP IF statement and make it work. Since I dont want the user to see the login form when they are already logged in.

 

Here is a link to the php script: http://pastebin.com/aenq4gbE

 

Here is link to the live page: http://jfbsystems.co.uk/secure

 

Any help or ideas would be much appreciated!

 

JellyFishBoy

Link to comment
Share on other sites

In that case i believe you should use sessions.

 

When a user logs in (after the username and password are correct based on your db) you should have something like:

 

session_start();
$_SESSION['user'] = 1; 
//or
$_SESSION['user'] = $user_id; //which you get from your db table

 

now if you do that in the login process page then you can have this in your homepage:

session_start();
if(isset($_SESSION['user']))
{
     //show Logout link and user info
}
else
{
     //show your login form
}

Link to comment
Share on other sites

<div style="position:absolute; left:467px; top:684px; width:286px; height:154px; z-index:1;">
    <div id="frag_3" style="text-align:left;">
<?php
session_start();
if(isset($_SESSION['user']))
{
   echo "<p class='Body-text-P'><span class='Body-text-T'>You are now logged in, <b>".$_COOKIE['firstname']." ".$_COOKIE['lastname']."</b></p></span>";
}
else
{
echo '<form id="form_1" name="login1" onSubmit="return validate_form_1(this)" action="logged.php" method="post" target="_self" enctype="application/x-www-form-urlencoded" style="margin:0px;">
<div id="txt_1" style="position:absolute; left:467px; top:570px; width:97px; height:16px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box; overflow:hidden; z-index:1;">
<p class="Body-text-P"><label for="edit_1"><span class="Body-text-T">Username</span></label></p>
</div>
<div style="position:absolute; left:550px; top:565px; width:150px; height:22px; text-align:left; z-index:1;">
    <input type="text" id="edit_1" name="uname" size="20" style="width:150px;" maxlength="50" value="">
</div>
<div id="txt_2" style="position:absolute; left:467px; top:605px; width:101px; height:16px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box; overflow:hidden; z-index:1;">
<p class="Body-text-P"><label for="edit_2"><span class="Body-text-T">Password</span></label></p>
</div>
<div style="position:absolute; left:550px; top:600px; width:150px; height:22px; text-align:left; z-index:1;">
    <input type="password" id="edit_2" name="password" size="20" style="width:150px;" maxlength="100" value="">
</div>
<div style="position:absolute; left:547px; top:635px; width:81px; height:22px; text-align:left; z-index:1;">
    <input type="submit" name="submit" value="Submit">
<input type="hidden" name="submitlog" value="TRUE">
</div>
</form>';
     echo '<p class="Body-text-P"><span class="Body-text2-T">You are not logged in at this current time. Please login or register.</p></span>';
}
?> 
    </div></div>

 

Still no luck? :/ Div tags dont close with PHP tags between them and form doesnt even show up...

Link to comment
Share on other sites

You must declare session_start(); before you process any output to the browser. try removing it from whjere you have it and putting it at the top of the code inside it's own <?php  ?> tags.

 

Also, thyat's a rather epic echo statement, think about alternatives to that (like include)

 

As for the <div> tags not closing, it's never happend to me before... :shrug:

Link to comment
Share on other sites

Tried inputing include:

 

 

<div style="position:absolute; left:467px; top:684px; width:286px; height:154px; z-index:1;">
    <div id="frag_3" style="text-align:left;">
<?php
if(isset($_SESSION['user']))
{
   echo "<p class='Body-text-P'><span class='Body-text-T'>You are now logged in, <b>".$_COOKIE['firstname']." ".$_COOKIE['lastname']."</b></p></span>";
}
else
{
include 'lform.html';
     echo '<p class="Body-text-P"><span class="Body-text2-T">You are not logged in at this current time. Please login or register.</p></span>';
}
?> 
    <><>

 

 

But still now show, however when you look at page source the form html data is there, so im puzzled as to why there is no show o.O

 

 

lform.html has this inside...

 

 

<html>
<body>
<form id="form_1" name="login1" onSubmit="return validate_form_1(this)" action="logged.php" method="post" target="_self" enctype="application/x-www-form-urlencoded" style="margin:0px;">
<div id="txt_1" style="position:absolute; left:467px; top:570px; width:97px; height:16px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box; overflow:hidden; z-index:1;">
<p class="Body-text-P"><label for="edit_1"><span class="Body-text-T">Username</span></label></p>
<>
<div style="position:absolute; left:550px; top:565px; width:150px; height:22px; text-align:left; z-index:2;">
    <input type="text" id="edit_1" name="uname" size="20" style="width:150px;" maxlength="50" value="">
<>
<div id="txt_2" style="position:absolute; left:467px; top:605px; width:101px; height:16px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box; overflow:hidden; z-index:1;">
<p class="Body-text-P"><label for="edit_2"><span class="Body-text-T">Password</span></label></p>
<>
<div style="position:absolute; left:550px; top:600px; width:150px; height:22px; text-align:left; z-index:2;">
    <input type="password" id="edit_2" name="password" size="20" style="width:150px;" maxlength="100" value="">
<>
<div style="position:absolute; left:547px; top:635px; width:81px; height:22px; text-align:left; z-index:2;">
    <input type="submit" name="submit" value="Submit">
<input type="hidden" name="submitlog" value="TRUE">
<>
</form>
</body>
</html>

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.