JasperHope Posted April 17, 2010 Share Posted April 17, 2010 I'm creating a blog for a website, so I need a text box to add its content to a div script. How would this be done? Also, I have created a basic password system with PHP, when the user enters the correct password, the PHP will return to the index. However, how do I make the HTML know when the password was correct? Hopefully changing the index page allowing the user to see and use a text box. Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/ Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 Generate a session for the user, then it's as simple as ABC. <?php session_start(); //Required on pages using sessions if (isset($_SESSION['userid'])) { print "You are logged in, here is the form"; ?> <form ...> </form...> <?php } else { print "Please log in before using this form"; } ?> You should read up on the basics, as this should be fairly simple to do. Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043623 Share on other sites More sharing options...
JasperHope Posted April 17, 2010 Author Share Posted April 17, 2010 Ah that makes sense! Thanks. What is the userid? Does that find the IP or the username from the login? Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043632 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 Read up on sessions here: http://www.php.net/manual/en/book.session.php http://www.tizag.com/phpT/phpsessions.php I assume you know how to handle the $_POST data from the forms, Match it and if the password is correct use the following line (very simple example): if ($_POST['user'] == "Mark" && $_POST['password'] == "123") { $_SESSION['userid'] = "Mark"; } Of course with session_start at the beginning of each file. On the index the $_SESSION superglobal will have the user ID set (in userid), thus the index can tell if the user is logged in or not. Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043634 Share on other sites More sharing options...
JasperHope Posted April 17, 2010 Author Share Posted April 17, 2010 So would this be placed in my index.php?: if (isset($_SESSION['userid']) == 'Admin') { print "You are logged in, here is the form"; ?> <html> blah blah </html> <?php }?> The if condition, is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043642 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 Your brace ( } ) is not enclosed within the PHP tags so it will be useless, but otherwise yes, that will work. Be aware anything below the brace (Not in an ELSE statement as your example) will be visible to any user, you should really read up on the basics before you tackle anything harder. Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043646 Share on other sites More sharing options...
webmaster1 Posted April 17, 2010 Share Posted April 17, 2010 Example of an if condition: <html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html> List of operators that can be used in your if condition (within the set of brackets after the word 'if'): http://www.w3schools.com/php/php_operators.asp Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043648 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 The if condition, is that correct? Your new edit on the code is correct, in the point being it will only show if userid is set to "Admin". Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043652 Share on other sites More sharing options...
JasperHope Posted April 17, 2010 Author Share Posted April 17, 2010 Thanks for the replies. It seems to work! However, this code only returns "You are not Admin". I assume I need to make a cookie with the userid stored? <?php session_start(); if (isset($_SESSION['userid']) == 'Admin') { echo "You are Admin"; } else { echo "You are not Admin"; } Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043692 Share on other sites More sharing options...
oni-kun Posted April 17, 2010 Share Posted April 17, 2010 Your code uses isset and a comparison operator at the same time, which cannot be (Unless testing for TRUE or FALSE) if (isset($_SESSION['userid']) == 'Admin') { Should be: if (isset($_SESSION['userid']) AND $_SESSION['userid'] == 'Admin') { You can read more about operators here: http://php.net/manual/en/language.operators.comparison.php Cookies are not needed, Sessions are kept on the server (Only a cookie with the session ID is set to the user) and can be changed to what you wish. Again, the basics are really of use. Quote Link to comment https://forums.phpfreaks.com/topic/198832-text-box-adding-text-to-html-script-html-php-checker/#findComment-1043945 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.