Hello world !

First of all, i'd like to say that this is my first page in PHP, so i may have made the most basic of mistakes :)

I wrote most of this from a tutorial i found somewhere on the web.

I get the following errors :


Notice: Undefined index: logged in (...)\index.php on line 7

Notice: Undefined index: user in (...)\index.php on line 67

Notice: Undefined index: pass in (...)\index.php on line 67

I understand the meaning of thoses messages, i think, which is that the variables are not set. I don't understand however how i can fix this.

Any help would be greatly appreciated :)

<?php

session_start();


if($_SESSION["logged"])

	{

	print_secure_content();

	}

else {
   if(!$_SESSION["logging"])
   	{  

		$_SESSION["logging"]=true;

		loginform();

		}

	else if($_SESSION["logging"])

		{

		$number_of_rows=checkpass();

		if($number_of_rows==1)

			{

			$_SESSION["user"]=$_GET[userlogin];
			$_SESSION["logged"]=true;
			print"You are now logged in.";
			print_secure_content();
			}
		else{
			print "wrong password or username, please try again";
			loginform();
			}
		}

	}
	
function loginform()
	{
	echo
		" 
		Please login <BR>
		username : <input type='text' name='user' size='20'> <BR>
		password : <input type='password' name='pass' size='20'> <BR>
		<input type='submit'> <BR>
		Or register now !
		";
	}

function checkpass()
	{
	$servername="127.0.0.1";
	$mysqluser="root";
	$mysqlpass="mysql";
	$conn=mysql_connect($servername,$mysqluser) or die (mysql_error());
	mysql_select_db("profit",$conn);
	$sql="select * from users where user='$_GET[user]' and pass='$_GET[pass]'";
	$result=mysql_query($sql,$conn) or die (mysql_error());
	return mysql_num_rows($result);
	}

function print_secure_content()
	{
	echo 
		"
		Status : Logged in as $_SESSION[user] <BR>
		Logout...
		";
	}

?>

Recommended Answers

All 8 Replies

Member Avatar for diafol

try using

if(isset($_SESSION['logged']) && $_SESSION['logged']){

If the variable is not set at all, php will not know what to do with it. Methinks.

Thank you so much for the tip.

It looks like it was about to work, but somehow now i just get this error when trying to load the page :

Fatal error: Maximum execution time of 30 seconds exceeded in (...)\index.php on line 3

<?php

session_start();


    if(isset($_SESSION['logged']) && $_SESSION['logged'])
   {

		print_secure_content();

	}

else {
   if(!$_SESSION["logging"])
   	{  

		$_SESSION["logging"]=true;

		loginform();

		}

	else if($_SESSION["logging"])

		{

		$number_of_rows=checkpass();

		if($number_of_rows==1)

			{

			$_SESSION["user"]=$_GET[userlogin];
			$_SESSION["logged"]=true;
			print"You are now logged in.";
			print_secure_content();
			}
		else{
			print "wrong password or username, please try again";
			loginform();
			}
		}

	}
	
function loginform()
	{
	echo
		" 
		Please login <BR>
		username : <input type='text' name='user' size='20'> <BR>
		password : <input type='password' name='pass' size='20'> <BR>
		<input type='submit'> <BR>
		Or register now !
		";
	}

function checkpass()
	{
	$servername="127.0.0.1:8887";
	$mysqluser="root";
	$mysqlpass="mysql";
	$conn=mysql_connect($servername,$mysqluser) or die (mysql_error());
	mysql_select_db("profit",$conn);
	$sql="select * from users where user='$_GET[user]' and pass='$_GET[pass]'";
	$result=mysql_query($sql,$conn) or die (mysql_error());
	return mysql_num_rows($result);
	}

function print_secure_content()
	{
	echo 
		"
		Status : Logged in as $_SESSION[user] <BR>
		Logout...
		";
	}

?>

Any clues?

Oh i did look around, and most people suggest i should increase the number of seconds before time out.

It didn't feel like the right option though, because as i understand it, it means my code is taking more than 30 seconds to execute, which is already way too long.

Is there a reason that my code is so slow?

Or will it speed up after i increase the time limit somehow?

try rebooting server may be (if local). Cant see anything big with code unless you have loops in your "secret" code !

I'm using EasyPHP.

I tried restarting the server, and even rebooting the machine, but that's not working :(

This is the error message :

Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in M:\EasyPHP-5.3.8.1\www\Dropbox\Profit\index.php on line 59

Warning: mysql_connect() [function.mysql-connect]: Error while reading greeting packet. PID=3660 in M:\EasyPHP-5.3.8.1\www\Dropbox\Profit\index.php on line 59

Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in M:\EasyPHP-5.3.8.1\www\Dropbox\Profit\index.php on line 59

Fatal error: Maximum execution time of 180 seconds exceeded in M:\EasyPHP-5.3.8.1\www\Dropbox\Profit\index.php on line 59

and this is the full code :

<html>

<head>

<title>

Titles are good for entitlement

</title>

</head>

<body>



<?php

set_time_limit(180);

session_start();


    if(isset($_SESSION['logged']) && $_SESSION['logged'])
   {

		print_secure_content();

	}

else {
   if(!$_SESSION["logging"])
   	{  

		$_SESSION["logging"]=true;

		loginform();

		}

	else if($_SESSION["logging"])

		{

		$number_of_rows=checkpass();

		if($number_of_rows==1)

			{

			$_SESSION["user"]=$_GET[userlogin];
			$_SESSION["logged"]=true;
			print"You are now logged in.";
			print_secure_content();
			}
		else{
			print "wrong password or username, please try again";
			loginform();
			}
		}

	}
	
function loginform()
	{
	echo
		" 
		Please login <BR>
		username : <input type='text' name='user' size='20'> <BR>
		password : <input type='password' name='pass' size='20'> <BR>
		<input type='submit'> <BR>
		Or register now !
		";
	}

function checkpass()
	{
	$servername="127.0.0.1:8887";
	$mysqluser="root";
	$mysqlpass="mysql";
	$conn=mysql_connect($servername,$mysqluser) or die (mysql_error());
	mysql_select_db("profit",$conn);
	$sql="select * from users where user='$_GET[user]' and pass='$_GET[pass]'";
	$result=mysql_query($sql,$conn) or die (mysql_error());
	return mysql_num_rows($result);
	}

function print_secure_content()
	{
	echo 
		"
		Status : Logged in as $_SESSION[user] <BR>
		Logout...
		";
	}

?>





</center>

</body>

</html>

It seems there is issue with server you are using. I would suggest you install WAMP/XAMPP to avoid the issue!

Ok thanks !
From what you said i checked the server and found out that my database had disappeared from phpmyadmin. I re-created it and i no longer get the error.

On the other hand, i still have an issue with it saying :

"Notice: Undefined index: userlogin in (...)\index.php on line 84"

and

"Notice: Undefined index: password in (...)\index.php on line 84"

From what i understand, it is saying that userlogin and password don't exist. I guess that it's true, since when i first load the page, nothing has been inputted in the form.

But that bit of code (checkpass) should only run when session is logging, which is supposed to happen after the user has filled in the form and clicked submit.

Am i understanding this right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.