LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-26-2013, 04:42 PM   #1
ExemptOA
LQ Newbie
 
Registered: May 2012
Posts: 13

Rep: Reputation: Disabled
Cannot get PHP scripts to work in Ubuntu Server 12.04


Here's what is installed that i will be utilizing:
-PHP5
-Apache2
-MySql

I have tried multiple ways of getting a PHP login script to work (ie: connects to mysql db pulls login credentials and authenticates user with a session) When i try to get this to work however, i get this generic error on my web browser.

Server error
The website encountered an error while retrieving http://intranet/login.php?attempt. It may be down for maintenance or configured incorrectly.


Here is the PHP script along with the HTML form i am using.

*******************************************************
<?php

if(isset($_REQUEST['attempt']))
{
$user = $_POST['user'];
$password = $_POST['password'];

$link = mysql_connect('localhost', 'root', 'mypassword') or die('Cant connect to database');
mysql_select_db('test_users');

$query = mysql_query("SELECT * FROM users") or die(mysql_error());


}



?>


<form method="POST" action="login.php?attempt">
User:<input type="test" name="user"/>
Pass:<input type="password" name="password"/>
<input type="submit"/>


</form>

*******************************************************

I am not currently worried about the security aspect as of yet. all i want is for it to work and i can get more in depth later on..

any help would be great.
 
Old 05-26-2013, 05:01 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,399

Rep: Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958
Quote:
Originally Posted by ExemptOA View Post
Here's what is installed that i will be utilizing:
-PHP5
-Apache2
-MySql

I have tried multiple ways of getting a PHP login script to work (ie: connects to mysql db pulls login credentials and authenticates user with a session) When i try to get this to work however, i get this generic error on my web browser.

Server error
The website encountered an error while retrieving http://intranet/login.php?attempt. It may be down for maintenance or configured incorrectly.

Here is the PHP script along with the HTML form i am using.
Code:
<?php
if(isset($_REQUEST['attempt']))
{
$user = $_POST['user'];
$password = $_POST['password'];

$link = mysql_connect('localhost', 'root', 'mypassword') or die('Cant connect to database');
mysql_select_db('test_users');

$query = mysql_query("SELECT * FROM users") or die(mysql_error());
}
?>

<form method="POST" action="login.php?attempt">
	User:<input type="test" name="user"/>
	Pass:<input type="password" name="password"/>
	<input type="submit"/>

</form>
I am not currently worried about the security aspect as of yet. all i want is for it to work and i can get more in depth later on.. any help would be great.
Here's a chunk of code I use for the same thing, and it works fine. There are a few differences, but you should be able to follow things.
Code:
<?php
session_start();
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {

     // I'm reading a config file for login parameters...modify as needed.
     $ini_array = parse_ini_file("config.ini", TRUE);
     $dbl=$ini_array['section1']['DBLoginID'];
     $dbp=$ini_array['section1']['DBLoginPW'];
     $link = mysql_connect("localhost", "$dbl", "$dbp") or die ("Could not connect to database; contact administrator.<BR>");
     $db = "databasename";
     mysql_select_db($db) or die ("Could not select database; contact administrator<BR>");

     // Get the user ID/password from the form...
     $userId   = $_POST['txtUserId'];
     $password = $_POST['txtPassword'];

     // check if the user id and password combination exist in database
     $sql = "SELECT user_id FROM tbl_auth_user WHERE user_id = '$userId' AND user_password = PASSWORD('$password') and user_level = 'Y'";
        $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
     if (mysql_num_rows($result) == 1)
           {
        // the user id and password match, set the session
        $_SESSION['db_is_logged_in'] = true;
        $_SESSION['auth_admin'] = true;
        $_SESSION['userid']=$userId;

        // after login we move to the main page
        header('Location: /some/path/nextpage.php');
                exit;
       }
        else
         {
          // The id/password is wrong.  Dump the user out, and close the DB connection.
          $errorMessage = 'Sorry, wrong user id / password, or permission denied.  Check user ID and password, or contact administrator.';
         }
     mysql_close();
}
?>

<html><head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if ($errorMessage != '') {
?>

<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>

<?php
}
?>
<form action="" method="post" name="frmLogin" id="frmLogin">
 <table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
  <tr>
   <td width="150">User Id</td>
   <td><input name="txtUserId" type="text" id="txtUserId"></td>
  </tr>
  <tr>
   <td width="150">Password</td>
   <td><input name="txtPassword" type="password" id="txtPassword"></td>
  </tr>
  <tr>
   <td width="150">&nbsp;</td>
   <td><input name="btnLogin" type="submit" id="btnLogin" value="Login"></td>
  </tr>
 </table>
</form>
</body></html>
Could be neater, but this works, and is only an example to get you going. You're on the right track.
 
1 members found this post helpful.
Old 05-26-2013, 05:10 PM   #3
ExemptOA
LQ Newbie
 
Registered: May 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Alright thanks. Ill give it a try. Also when i was installing the applications on the ubuntu server, i did not make any changes to config files at all. Is there any configuration needed to make this work?
 
Old 05-26-2013, 05:29 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,399

Rep: Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958
Quote:
Originally Posted by ExemptOA View Post
Alright thanks. Ill give it a try. Also when i was installing the applications on the ubuntu server, i did not make any changes to config files at all. Is there any configuration needed to make this work?
Well, if you already have PHP code working now, and you have verified that MySQL is working, then you're probably ready to go.
 
Old 05-26-2013, 07:16 PM   #5
ExemptOA
LQ Newbie
 
Registered: May 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Ok i went to my Apache2 error log and this is what the error reads..

PHP Fatal error: Call to undefined function mysql_connect() in /var/www/login.php on line 9, referer: http://intranet/index


Here is my login.php script
*********************************************************
<?php

session_start();
$username = $_POST['username'];
$password = $_POST['password'];

if($username && $password)
{
$connect = mysql_connect("localhost","root","Ybhar55c09f2") or die("Couldnt Connect to DB");
mysql_select_db("login") or die ("Couldnt find DB");

$query = mysql_query("SELECT * FROM users WHERE username = '$username'");

$numrows = mysql_num_rows($query);

if($numrows !=0)
{

while($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username==$dbusername&&$password==$dbpassword)
{
echo "Login successful <a href='membersarea.php'>Click here to enter members area.</a>";
$_SESSION['username']=$dbusername;

}
else
echo "The username or password is incorrect";

}

else
die("That username does not exist");

}
else
die("Enter a username and password");


?>
*******************************************************************

Here is the HTML file (referer)

*******************************************************************

<html>

<form action="login.php" method="POST">
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" name="submit" value="Login">

</form>


</html>

*******************************************************************


I have no idea what is wrong. When i use XAMPP on my local machine this entire script works fine but then when i transfer these files to my server they dont work. Yes, i am changing my login credentials to the database when i transfer the files..


Also: I have PHP script in my other files that dont need the database to work, and they are fine. When the errors happen, is when my PHP tries to pull data from the MySql database.

Last edited by ExemptOA; 05-26-2013 at 07:21 PM.
 
Old 05-26-2013, 07:53 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,537

Rep: Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420
This
Quote:
Call to undefined function mysql_connect()
says you haven't installed the pkg that contains that fn.
It'd probably php_mysql or some such name.
 
1 members found this post helpful.
Old 05-26-2013, 08:29 PM   #7
ExemptOA
LQ Newbie
 
Registered: May 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Ok that was exactly my problem. installed needed packages and it now recognizes MySql. Thanks for the help man!
 
Old 05-26-2013, 11:17 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,537

Rep: Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420
Good
Don't be afraid of errors; just read them carefully; they're often all you need to know
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Ubuntu Server 12.04+PHP Server Side Script+Android Client App]Stripslashes not work! MarkoSan Linux - Server 0 05-24-2012 08:35 AM
How do I make openal work with php on ubuntu server 9.04? MadMusician_uk Ubuntu 0 11-18-2009 06:07 PM
HELP! - PHP scripts won't work after reinstall of Apache linuxfond Linux - Server 3 11-22-2008 05:03 AM
is there way to run php scripts without having any server os...... kratosal Linux - Newbie 5 04-06-2008 02:30 PM
Running PHP Scripts in SuSe Linux 9.3 w/o a server. elliotfuller Linux - Software 3 06-18-2005 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration