JellyFishBoy Posted February 10, 2011 Share Posted February 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/ Share on other sites More sharing options...
solon Posted February 10, 2011 Share Posted February 10, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1172271 Share on other sites More sharing options...
JellyFishBoy Posted February 10, 2011 Author Share Posted February 10, 2011 <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... Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1172315 Share on other sites More sharing options...
Muddy_Funster Posted February 10, 2011 Share Posted February 10, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1172318 Share on other sites More sharing options...
JellyFishBoy Posted February 10, 2011 Author Share Posted February 10, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1172326 Share on other sites More sharing options...
cgeisler515 Posted February 10, 2011 Share Posted February 10, 2011 I would recommend this tool to you. you drop in html code and it spits out properly formatted php echo statments. http://www.yellowpipe.com/yis/tools/HTML_converter/ Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1172511 Share on other sites More sharing options...
JellyFishBoy Posted February 28, 2011 Author Share Posted February 28, 2011 Realised I had too many div positioning tags which conflicted. Ended up using the above tool linked, worked a treat! Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/227250-html-form-inside-php-if-statement/#findComment-1180804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.