Jump to content

HTML form Within PHP tags


Boldonglen

Recommended Posts

Hello firstly i would like to say im very new to this forum (Signed up 5 minutes ago). I have decided to join as im in my final year at university and have been asked to create a website, so im guessing ill be coming back here a lot for help! :P.

 

Anyway my most current problem is i have an if statement that hides the "Register" button if the user is logged in and Shows a welcome message and the users name if the user is logged in. However i would like to make the log in form disappear if the user is logged in. I know that by putting the form code in the else part of my if statement it will disappear if the user is logged in however when i do this it stops my whole website working.

 

Any help would be very grateful.

 

Thanks Glen.

Link to comment
https://forums.phpfreaks.com/topic/230348-html-form-within-php-tags/
Share on other sites

Im sorry im new to this forum forgot to put the code in Here is my code:

 

<?php

		session_start();

		if ($_SESSION['username'])
			echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>";
		else
			echo "<a href='register.php'>Register?</a>";
			  
		?>

		<form action='login.php' method="post">
		Username: 
		<input type="text" name="username">
		<br>
		Password: 
		<input type="password" name="password">
		<br>
		<input type="submit" value="Log in">
		</form>

 

I would like to put the form above into the else part of the if statement.

I don't know if this will work, but try:

<?php

		session_start();

		if ($_SESSION['username'])
			echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>";
		else
			echo "<a href='register.php'>Register?</a>";\
echo "
		<form action=\"login.php\" method=\"post\">
		Username: 
		<input type=\"text\" name=\"username\">
		<br>
		Password: 
		<input type=\"password\" name=\"password\">
		<br>
		<input type=\"submit\" value=\"Log in\">
		</form>  
		?>

again, I don't know if that is what you want, but it could be......

 

try something like:

<?php
session_start(); // Must start session first thing

// Here we run a login check
if (!isset($_SESSION['id'])) {
echo 'Please <a href="login.php">log in</a> to access your account';
exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$id = $_SESSION['id'];
?>

Try something like this and see if it works.

just try to stick that code in at the beginning of the page, but you need to create a file called connect_to_mysql.php:

 

<?php 

// Place db host name. Usually is "localhost" but sometimes a more direct string is needed
$db_host = "db_host";
// Place the username for the MySQL database here
$db_username = "username"; 
// Place the password for the MySQL database here
$db_pass = "password";
// Place the name for the MySQL database here
$db_name = "dbname";

mysql_connect("$db_host","$db_username","$db_pass") or die(mysql_error());
mysql_select_db("$db_name") or die("no database by that name");
?>

 

I understand that a previous page may work, but the page that you put the connection on is the only one that is gonna connect.  You need to put a connect on everypage as I have always been told.

If you want to include more than one line of code in a condition you must wrap it in { ... }

 

<?php
session_start();

if ($_SESSION['username']) {
  echo "Welcome, ".$_SESSION['username']."! <br><a href='logout.php'>Logout!</a>";
} else {
?>
<a href='register.php'>Register?</a>
<form action='login.php' method="post">
Username: 
<input type="text" name="username">
<br>
Password: 
<input type="password" name="password">
<br>
<input type="submit" value="Log in">
</form>
<?php
}
?>

I understand that a previous page may work, but the page that you put the connection on is the only one that is gonna connect.  You need to put a connect on everypage as I have always been told.

 

You only need to have connection code on a page where you are actually wanting to make use of a database.  Judging by his code, he has his connection code, authenticates the user with it, and if everything is valid, he starts a session variable.  Then on subsequent pages he checks if that session variable exists.  You do not need to have database connection code on every page for this.  Sessions and databases are two different things.

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.