Jump to content

Only displaying html form if user is logged in.


fredted40x

Recommended Posts

Hi i have a simple form,

 

<form action="***.php" method="POST">
          ***:<br><textarea cols="60" rows="5" name="***"></textarea><br>
          <input type="submit" value="***">
        </form>

 

(have removed names)

 

Is it possible to hide this form if a session has not been created , i have tried using php

if ($_SESSION['user'])

but the form doesnt run inside the <?php> and ?> tags.

 

Anyone got any sugestions on what could solve this problem?

 

thanks

Yes PHP is perfect for simple things like this.

You almost had it right, here let me help you:

 

 

Notes:

isset() is a function to check wether a variable with the specified name exists at all (even if its empty).

empty() is a function to check wether a variable that has been created has data or content.

 

 

somepage.php

<?php

session_start();

if(isset($_SESSION['user']) && !empty($_SESSION['user'])){
  // Do not dipslay the form is user is logged in:
  echo("You are already logged in");
}else{
  // Show form etc.
  echo('
<form action="login.php" method="POST">
          Username:<br><textarea cols="60" rows="5" name="user"></textarea><br>
          <input type="submit" value="Login">
        </form>
  ');
}

?>

 

login.php

<?php

if($_POST['user'] == "somevaliduser"){
  echo("Logged in, Thanks.");
  $_SESSION['user'] = $_POST['user'];
}else{
  echo("Not Authorised");
}

?>

 

Hope this gives you an idea on how php is so powerful.

-CB-

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.