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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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.

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.