Jump to content

Recommended Posts

Hello all,

I am a PHP newbie and recently I started to make my own CMS, however already after 1 page I discovered that there is a mistake in my code which I can't find therefore I'd like to ask you all if you could help me please.

 

Here's my code:

<?php

//This file contains the whole script.

// File: includes/functions.php
function db_connect()
{
	$host = 'localhost';
	$user = 'root';
	$password = '';
	$database = 'test';

	$connection = mysql_connect($host,$username,$password) or die ('Error while executing the query, can not connect to the database.');
	$database = mysql_select_db($database, $connection) or die ('Error while executing the query, can not select database.');

	return $database;
}


//File .../Login.php

include('includes/functions.php');
if (isset($_POST['username']) and isset($_POST['password']))
	{
		db_connect();

		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$tmp = mysql_query('SELECT username FROM users WHERE username = "$username" AND password = "$password"');
		$num = mysql_num_rows($tmp);

		if ($num < 0 or $num > 1)
			{
				exit("Wrong login information. Please try again.");
			}

		$tmp = mysql_query('SELECT staff FROM users WHERE username = "$username"');

		switch($tmp)
			{
				case "1";
				session_start();
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "1";
				break;

				case "2";
				session_start();
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "2";
				break;

				default:
				break;
			}
			echo '<meta HTTP-EQUIV= "REFRESH" content="0"; url=http://localhost/index.php">';

		}

		else {
		print "No data entered. Please return and enter your login information in the required fields.";
		}

//////////////////////////////////////////////////////////// end. ///////////////////////////////////////////////
?>

 

Thanks a lot in advance.

Yours John_S

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/
Share on other sites

What have you done to troubleshoot where the problem is? What is it doing that is incorrect?

 

Programming help forums can only help with specific problems and can only help when you provide sufficient information about what you see in front of you.

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-641242
Share on other sites

Seems ok for me:

 

<form id="user_login" name="user_login" method="post" action="login.php">
            <label>Username <input type="text" name="username" id="username" /></label>
            <label>Password  <input type="password" name="password" id="password" /></label><br />
            <label>Submit  <input type="submit" name="submit" id="submit" value="Submit" /></label>
         </form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-641324
Share on other sites

try this

<?php
				session_start();

//This file contains the whole script.

// File: includes/functions.php
function db_connect()
{
	$host = 'localhost';
	$user = 'root';
	$password = '';
	$database = 'test';

	$connection = mysql_connect($host,$username,$password);
	$database = mysql_select_db($database, $connection);
	if($connection && $database)
	{
	return true;
	}
	else
	{
	$error = "Error while executing the query";
	return $error;
	}
}


//File .../Login.php

include('includes/functions.php');
if (isset($_POST['submit']))
	{
		echo db_connect();

		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$tmp = mysql_query('SELECT username FROM users WHERE username = "$username" AND password = "$password"') or die(mysql_error());
		$num = mysql_num_rows($tmp) or die(mysql_error());

		if ($num < 0 or $num > 1)
			{
				exit("Wrong login information. Please try again.");
			}

		$tmp = mysql_query('SELECT staff FROM users WHERE username = "$username"') or die(mysql_error());

		switch($tmp)
			{
				case "1";
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "1";
				break;

				case "2";
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "2";
				break;

				default:
				break;
			}
			echo '<meta HTTP-EQUIV= "REFRESH" content="0"; url=http://localhost/index.php">';

		}

		else {
		echo "No data entered. Please return and enter your login information in the required fields.";//i dont like print 
		}

//////////////////////////////////////////////////////////// end. ///////////////////////////////////////////////
?>

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-641329
Share on other sites

I did as you said and it seems to be working but only a part of the script, it does the switch and the redirect function, but it doesn't initiate session variables :( So I arrive back on index.php page without the session variables set. I think it is a MySQL query error but I don't see where could it be  ???

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-641362
Share on other sites

The problem lies in your switch statement

case "1";

session_start();

$_SESSION['user'] = $_POST['username'];

$_SESSION['permissions'] = "1";

break;

 

Put a colon [:] instead of semi-colon [;].

 

case "1"; should be case "1":

...

...

...

 

A semi-colon ends a php statement. Hence it seems that the moment you start a case it ends.

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-641775
Share on other sites

Here it is:

if (isset($_POST['username']) and isset($_POST['password']))
	{
		db_connect();

		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$tmp = mysql_query('SELECT username FROM users WHERE username = "$username" AND password = "$password"');
		$num = mysql_num_rows($tmp);

		if ($num < 0 or $num > 1)
			{
				exit("Wrong login information. Please try again.");
			}

		$tmp = mysql_query('SELECT staff FROM users WHERE username = "$username"');

		switch($tmp)
			{
				case "1";
				session_start();
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "1";
				break;

				case "2";
				session_start();
				$_SESSION['user'] = $_POST['username'];
				$_SESSION['permissions'] = "2";
				break;

				default:
				break;
			}
			echo '<meta HTTP-EQUIV= "REFRESH" content="0"; url=http://localhost/index.php">';

		}

 

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-643094
Share on other sites

You need to fetch the second query's results.  You also need to swap your double and single quotes around:

$tmp = mysql_query("SELECT staff FROM users WHERE username = '$username'");
$row = mysql_fetch_assoc($tmp);
$staff = $row['staff'];

switch($staff)
{
   case "1":
      session_start();
      $_SESSION['user'] = $_POST['username'];
      $_SESSION['permissions'] = "1";
      break;

   case "2":
   .
   .
   .
}

Link to comment
https://forums.phpfreaks.com/topic/124194-help-with-php-newbie/#findComment-643102
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.