Jump to content

Text box adding text to HTML script & HTML PHP checker


JasperHope

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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";
}

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.