Beginner needs help getting a comment box to input data into database

I understand what you meant Richei.

I updated my code to match you code but for some reason once I had done this my comments box was no longer visable on screen. Even stranger was when I changed my code back to the original code I was using the comments box still was not visable.

Also, I have been following a tutorial to create a content managment system and I had created a login.php page with a form which was visable when I initially created it. Then after trying to implement the php code needed to check my database for the login details the form was suddenly no longer visable on my login.php page.

I only mentioned this other case as I wondered if it might be something to do with MAMP that I havent set up correctly or something universal between the comment box and login page.

Thank you again for your help. Any advice is grealty appreciated!

Well, if want, ill do some 1on 1 with,u to figure this out

If it helps to figure it out, I removed all the php from the login.php page I created and that allowed my form to become visable again. Any ideas?

Well, there have been many many requests to help with login projects. We can help.
But, it is much easier to see the code. Please post the login page and the PHP code page.
Just make sure not to show your log-in connection info for your database.

Then, we can suggest how to fix it.

Thank you for the offer, feel a bit lost with problem solving as im a little new to PHP and MySQL.

Ill give you the three pages ive been working with:

login.php
[php]

Basic CMS - Admin Area - Login <?php session_start(): if(isset($_SESSION['user'])) { header("Location: index.php"); } else { ?>
Username:
Password:
<?php } ?> [/php]

index.php
[php]

Basic CMS - Admin Area <?php session_start(): if(isset($_SESSION['user'])) { ?>

Logged In! Welcome <?php> echo $_SESSION['user']; ?>

<?php } else { header("Location: admin/login.php"); } ?> [/php]

dologin.php
[php]<?php
include(‘includes/functions.php’);

if(isset($_POSRT([‘login’]))

{



	if($_POST['username']))

	{
	
		if($_POST['password']))
	
		{
		
			$query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error());
		
			$user = mysql_fetch_array($query);

	
	if(md5($_POST['password'] == $user['Password'])
		{
		echo "Login successful";

		$_SESSION['user'] = $user['Username'};

		header("Location: index.php");

		}
		else

		{
		echo "Please check your login details!";

		include('login.php');

		}

	} else

		{

		echo "Please check your password is correct!";

		include('login.php');

	}

}  else


	{

	echo "Please check your username is correct!";

	include('login.php');

} else

{

echo “Please check you filled out the login form!”;

include('login.php');

?>
[/php]

Hope these are laid out well enough, thanks for taking the time to help me out!

First, in the login.php, change this:
if(isset($_SESSION[‘user’]))
{
header(“Location: index.php”);
} else
{
to this:
if(isset($_SESSION[‘user’]))
header(“Location: index.php”);

(No else needed! As you just want an already logged in user to move on…)
And, remove this line near the end:

<?php } ?>

(not needed)

In dologin.php, the first test for the submit is wrong… This:

if(isset($_POSRT([‘login’]))

Should be:

if(isset($_POST([‘login’]))

So, the dologin.php file would never do anything… Perhaps that is your problem!

Lastly, in index.php, the first section will not work. So, in login.php, if a user is logged in it sends them to the index.php file. All ok with that. In the index.php file, if they are not logged in you try to send them to the login.php page. BUT, you do this with headers which will not work after you have HTML on the page. Therefore, I suggest changing this to something more like this version:
[php]

<?PHP session_start(); if(!isset($_SESSION['user'])) header("Location: admin/login.php"); ?> Basic CMS - Admin Area Logged In! Welcome <?php> echo $_SESSION['user']; ?> [/php] NOTE: I changed the check for user logged in to NOT-user-logged-in... And, removed all the useless code. The new version of index.php, checks for a user NOT logged in and redirects them. If logged in, it goes on jsut as normal. Much simpler and easy to follow code... No if-else's... Hope that helped! Good luck...

Amazing help, thank you so much, im grasping more as I go, it’s just a been a bit of a challenge getting up and running.

Im sure I’ll have some more issues as I progress with this cms system. I hope you don’t mind advising me a bit more as I go.

Thanks again to all of you!

i’ll take a more indepth look when i get home, but one glaring error i see is in dologin, that very first POST is spelled wrong.

Gilestodd, no problem… If the next problem is in the same line, just continue here.
If it is a new problem, create a new post with a title that indicates the new problem.

Also, just for your info, you can search this site for answers to similarly and previously asked questions.
(Might save you a lot of time!)

Good luck…

I may have spoken to soon!

As I said im following a tutorial and I believe I need to get to the index.php page after logging in. Which I gather is the admin area. After logging in I am redirected to the dologin.php page and as such no message is displayed letting me know ive logged in correctly.

My guess is that the user name that you typed in is not in your database.

To debug this, first check your database for the user name and make sure it is spelled correctly.
(Capitals do matter!)

Next, make sure your code is reading the database correctly.
In the dologin.php page, you can place some debug code to see the flow of the various IF clauses…

You can add this command:
die(“Got here…”); just about anywhere and see if it gets to that point in the code.
If it does, then, move the got-here down a few lines and try again. When you find the point that the code is broken, look at the lines above it and figure out what is wrong…

If you can’t figure it out, repost the new versions of the three pages so we can see the updated versions.

Good luck…

What’s the address for the tutorial?

Well, I gave you changes before that didn’t get made. I will give you them again. Here is the login.php page:

[php]

<?php session_start(): if(isset($_SESSION['user'])) header("Location: index.php"); ?> Basic CMS - Admin Area - Login
Username:
Password:
[/php]

dologin.php :
[php]

<?php include('includes/functions.php'); if(isset($_POST(['login'])){ if($_POST['username'])){ if($_POST['password'])){ $query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error()); $user = mysql_fetch_array($query); if(md5($_POST['password'] == $user['Password']){ echo "Login successful"; $_SESSION['user'] = $user['Username'}; header("Location: index.php"); }else{ echo "Please check your login details!"; include('login.php'); } }else{ echo "Please check your password is correct!"; include('login.php'); } }else{ echo "Please check your username is correct!"; include('login.php'); }else{ echo "Please check you filled out the login form!"; include('login.php'); } ?>

[/php]

index.php :
[php]

<?php session_start(): if(!isset($_SESSION['user'])) header("Location: login.php"); ?> Basic CMS - Admin Area Logged In! Welcome <?php> echo $_SESSION['user']; ?> [/php] So, try those and if any other errors, let us know!

Will do, thanks again!

I updated my code to the code you provided on all three pages but im still stuck on the dologin.php page.

I entered die(“Got here…”); in various places throughout the dologin.php page but on every attempt “Got here…” was not visable on the dologin.php page.

Just to double check I wasn’t making a mistake with my login details I created another user with new details and tried to login again with the same outcome.

Also, to make the login form visable I needed to have the PHP code within the body of the html document. In a previous comment ErnieAlex you said that headers would not work after html code has been on the page, could this be the issue?

Also, as you requested Richei, the link to the second part of the tutorial I have been following…

http://www.youtube.com/watch?v=u3ry84gg0fw

Part 2 [Super Improved] is where I have reached. Also I was not able to redirect my localhost:8888/cms/admin to the login.php using the first piece of PHP he used incase that was important.

Quick update, ive been attempting to add logout and editing functions to my admin area, index.php, and im getting nothing showing on screen. Not even a simple

This is test text

is working…

Well, again, it is hard to fix it without your code. Please show us, inside PHP tags, your current code and we will try to fix you up…

I am working here on and off today and tomorrow morning, so send the code and I will look at it fairly soon!

Sorry, I didn’t post the code because it matched your previous code.

Login.php
[php]

Basic CMS - Admin Area - Login <?PHP session_start(); if(isset($_SESSION['user'])) header("Location: index.php"); ?>
Username:
Password:
[/php]

Dologin.php
[php]

<?php include('includes/functions.php'); start_session(); if(isset($_POST(['login'])){ if($_POST['username'])){ if($_POST['password'])){ $query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error()); $user = mysql_fetch_array($query); if(md5($_POST['password'] == $user['Password']){ echo "Login successful"; $_SESSION['user'] = $user['Username'}; header("Location: admin/index.php"); }else{ echo "Please check your login details!"; include('login.php'); } }else{ echo "Please check your password is correct!"; include('login.php'); } }else{ echo "Please check your username is correct!"; include('login.php'); }else{ echo "Please check you filled out the login form!"; include('login.php'); } ?>[/php]

Index.php
[php]

<?php session_start(); if(!isset($_SESSION['user'])) header("Location: admin/login.php"); ?> Basic CMS - Admin Area

This is test text....it does not appear on screen.

Logged In! Welcome <?php> echo $_SESSION['user']; ?>

Logout
Manage Posts

[/php]

I attempted to debug with die(“Got here…”); but after inserting this code in every available space in my dologin.php page, it was never visable after logging in. It appears that after logging in I am redirected to the dologin.php page and nothing happens from there.

Once again, ill be very greatful for any help

I’ve managed to get a “Login succesful” message on the dologin.php page, code as follows:

[php]<?php
include(‘includes/functions.php’);
session_start();

if (isset($_POST[‘login’])) {
if(isset($_POST[‘username’])) {
if(isset($_POST[‘password’])) {

		$username = $_POST['username'];
		$query = mysql_query("SELECT * FROM users WHERE Username = '$username'") or die (mysql_error());
		$user = mysql_fetch_array($query);
		
		if(md5($_POST['password']) == $user['Password']) {
			echo "Login successful";
			$_SESSION['user'] = $user['Username'];
			header("Location: index.php");
		} else {
			
			echo "Please check your login details!";
			include('login.php');
		}			
	} else {
		echo "Please check your password!";
		include('login.php');
	}
} else {
	echo "Please check your username!";
	include('login.php');
}

} else {
echo “Please check that you filled out the login form!”;
include(‘login.php’);
}
?>[/php]

Just cant get it to redirect me to the index.php page. header(“Location: index.php”); isn’t working I assume?

Sponsor our Newsletter | Privacy Policy | Terms of Service