Jump to content

PHP4 host but PHP5 code, don't know what to do?


budder

Recommended Posts

My php host site is runing php 4.4.4 and that is a problem because when i run the script in wamp on localhost there is no problem. But When I got it on the php host, problems popped up. I know there is alot of new function in php 5.02 >

but i don't think that is the problem, or is it?

<?php

functions.inc.php

function redirect($page) {

header('Location: ' . $page);

exit();

}

 

function check_login_status() {

// If $_SESSION['logged_in'] is set, return the status

if (isset($_SESSION['logged_in'])) {

return $_SESSION['logged_in'];

}

return false;

}

?>

 

login.inc.php

<?php

require_once('config.inc.php');

require_once('functions.inc.php');

session_start();

 

// Check if user is already logged in

if ($_SESSION['logged_in'] == true) {

// If user is already logged in, redirect to main page

redirect('../index.php');

} else {

// Make sure that user submitted a username/password and username only consists of alphanumeric chars

if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR

(!ctype_alnum($_POST['username'])) ) {

redirect('../login.php');

}

 

// Connect to database

$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

 

// Check connection

if (mysqli_connect_errno()) {

printf("Unable to connect to database: %s", mysqli_connect_error());

exit();

}

 

// Escape any unsafe characters before querying database

$username = $mysqli->real_escape_string($_POST['username']);

$password = $mysqli->real_escape_string($_POST['password']);

 

// Construct SQL statement for query & execute

$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";

$result = $mysqli->query($sql);

 

// If one row is returned, username and password are valid

if (is_object($result) && $result->num_rows == 1) {

// Set session variable for login status to true

$_SESSION['logged_in'] = true;

redirect('../index.php');

} else {

// If number of rows returned is not one, redirect back to login screen

redirect('../login.php');

}

}

?>

 

First there where no errors when i tried loggin in but after hitting enter it just the blank login.inc.php I get. Then I tried an easy error handling:

 

function customError($errno, $errstr)

{

echo "<b>Error:</b> [$errno] $errstr";

}

 

set_error_handler("customError");

 

at the top of my login.inc.php (After requiere and session_start()

Now i got something. There is and undefined index: logged_in. Aha!

 

So scripts is making an error because i have'nt defined the session and therefore it is returning NULL session.

 

Then i tried session_is_registred(logged_in) strangly the error messeage disapered but I still get the blank login.inc.php and I am getting pretty blank.

 

So if there is anyone who can help me out with this problem i will be very greatful.

 

//Budder

Link to comment
Share on other sites

Don't reverse your back to PHP 4.x.x just for the host. Because when they do upgrade, you'll have problems again.

Contact your webhost about upgrading to PHP 5.x.x or if they refuse to do this, or the time consuming of it is too long and your running out of bread (patience) then .. perhaps get a new webhost and ask them to transfer the files over from this host. :)

Link to comment
Share on other sites

The ONLY thing your code is doing that is php5 specific is using the mysqli extension (which would account for the blank pages because you would be getting a fatal runtime error.)

 

The end of life of php4 was over two years ago. Your web host should have provided a way of selecting php5, either through a control panel menu item or through adding a command in a .htaccess file.

 

Spend your time updating the account to php5, not wasting your time fixing non-problems in the code.

Link to comment
Share on other sites

Yeah your right guys, there is no reason to degrade my php script although I have'nt used any php5 :b

 

Im not the hostmaster og the site I only help with some coding. But i would certainly tell the hostmaster to change host.

 

So to fix the blank page I should use mysql_query instead?

Link to comment
Share on other sites

Yeah your right guys, there is no reason to degrade my php script although I have'nt used any php5 :b

 

Im not the hostmaster og the site I only help with some coding. But i would certainly tell the hostmaster to change host.

 

So to fix the blank page I should use mysql_query instead?

 

Yes, You should look at the mysql_query format. All in all it doesn't matter,

Performance wise it's in a fractional margin.

 

You should turn error reporting to E_ALL during development with display_errors on, It should provide useful information instead of blank screens as well.

 

Link to comment
Share on other sites

Thanks. Great answers. I will try it out tomorrow.

And thanks for the blog link (mysql vs. mysqli)

 

Does php4 support md5 og sha1? Or is it password(); ?

 

It supports md5, sha1 and crypt, Although crypt()'s underline coding may have changed through versions, you can check the documentation.

Link to comment
Share on other sites

Hey guys. Can you help me one more time?

 

Got this syntax error, unexpected $end in login.inc.php on line 46.

 

I have tried degrade my script (I have to i know it's stupid) but I am missing something?

<?php
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');

// Start session
function customError($errno, $errstr)
  {
  echo "<b>Error:</b> [$errno] $errstr";
  }

if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
} else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR
     (!ctype_alnum($_POST['username'])) ) {
	redirect('../login.php');
}
$mysqli = mysql_connect("$DB_HOSTNAME", "$DB_USERNAME", "$DB_PASSWORD");
mysql_select_db("DB_DATABASE", $mysqli) or die("Can not connect");
// Check connection
if (mysql_error()) {
	printf("Unable to connect to database: %s", mysql_error());
	exit();
}

// Escape any unsafe characters before querying database
$username = $_POST['username'];
$password = $_POST['password'];

// Construct SQL statement for query & execute
$sql	= "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "'";
$result = mysql_query($sql,$db);

// If one row is returned, username and password are valid
if (is_object($result) && mysql_num_rows == 1) {
	// Set session variable for login status to true
	$_SESSION['logged_in'] = true;
	redirect('../index.php');
} else {
	// If number of rows returned is not one, redirect back to login screen
	redirect('../login.php');
}
?>

 

Remember, its PHP4  :b

 

Link to comment
Share on other sites

However.. notepad++ when you go onto a new line, it doesn't indent it for you (like when you have an if statement) Dreamweaver does.

Sometimes this helps you work out where your going wrong 'curly brace' wise.

example.

//This is correct
if {
     if {
          if {
               'statement'
          }
     }
}

//This isn't correct
if {
     if {
          if {
               'statement'
          }
}

//This, is confusing to work out the problem. (notepad++ style, and no problem in this example)
if {
if {
if {
'statement'
}
}
}

Link to comment
Share on other sites

Alright guys I got my mysqli statements to mysql. But something doesn't work.

When trying to log in the script just send my back to the login.

Then I checked the data in the sql database. I got two users: test1 and test2

Test1 got md5 crypt and test2 doesn't. (I know I got my md5($password) in the script )

But i tried to chance the response so it didn't crypt the md5.

But neither way working its just sending me back to basic (login) have a look... again.

<?php
// Include required MySQL configuration file and functions
require_once('config.inc.php');
require_once('functions.inc.php');

// Start session

if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
} else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR
     (!ctype_alnum($_POST['username'])) ) {
	redirect('../login.php');
}
$mysqli = mysql_connect("$dbHost", "$dbUser", "$dbPass");
mysql_select_db($dbDatabase, $mysqli) or die("Can not connect");
// Check connection
if (mysql_error()) {
	printf("Unable to connect to database: %s", mysql_error());
	exit();
}

// Escape any unsafe characters before querying database. PHP4 doesn't 
$username = $_POST['username'];
$password = $_POST['password'];

// Construct SQL statement for query & execute Tried deleting md5(); to check test2 user, but nothing worked.
$result=mysql_query("select * from users where username='" . $username. "' AND password='" . md5($password) . "'", $mysqli);

// If one row is returned, username and password are valid
if (is_object($result) && mysql_num_rows == 1) {
	// Set session variable for login status to true
	$_SESSION['logged_in'] = true;
	redirect('../index.php');
} else {
	// If number of rows returned is not one, redirect back to login screen
	echo $result;
}
}
?>

Tried deleting md5(); to check test2 user, but nothing worked.

 

EDIT:

tried to echo the $result and it gives me:

Resource id #5

 

Shouldn't it be showing my sql-statement?

Link to comment
Share on other sites

However.. notepad++ when you go onto a new line, it doesn't indent it for you (like when you have an if statement) Dreamweaver does.

 

OMG no offense but you should be shot :P.

 

Notepad++ has one of the best line matching algorithms i know of. It follows your cursor up and down, indents code exactly how you indent it etc, nothing wrong with it at all.

 

-CB-

 

(Dreamwaver is pointless, get something free, You will learn 10x faster with plain text editors than with WYSIWYGS like dreamweaver)

Link to comment
Share on other sites

OMG no offense but you should be shot :P.

Shoot me and I shoot you twice. :)

 

Yeah, notepad++ has the best line matching, but it doesn't indent for you :) Actually I was doing some work using notepad++ I feel it is more of a developers tool than what dreamweaver is. Just annoying with the indenting xD

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.