Link to home
Start Free TrialLog in
Avatar of jericotolentino
jericotolentinoFlag for Philippines

asked on

Moving PHP scripts from a Windows environment to Linux

Dear Experts,

I'm pretty new to Linux but I can manage easy tasks like starting up and shutdown, installing software and saving files.

Anyway, my question is how do I make my PHP scripts developed in a Windows environment work with Linux?

I used sessions to login users into my site but I had to edit the php.ini file to make it work first. I'm not sure what the problem is really, but I can login, get into the first page (members homepage), but when I try to click on other pages that check if the session is valid, I'm redirected to the login page.

I'm using Mandrake 9.2, Apache 2.0, MySQL 4.0.18, and PHP 4.3

Thanks in advance!
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Firstly make sure all your script files have unix-format line breaks.

Apart from that, the usual sticking point of moving platforms is paths - paths like 'c:\stuff' will be meaningless on Linux, so check all your paths.

It sounds like your sessions are not starting - check that session files are being created in /tmp (or wherever you have told them to go in php.ini). We need more detailed info before we can be much help.
Avatar of jericotolentino

ASKER

Thanks for your reply. I checked the /tmp folder and there were files there. There's a file there starting with session and followed by alphanumeric characters. I'm not sure that's an indication that the sessions were starting but I saw a lot of those on Windows.

My paths are ok though. The page is redirecting well and the includes are all processed fine.

I'm not also sure by what you mean by "unix-format line breaks." Are you talking about <br />?

Sorry, this is a little too hard for me...
Avatar of robknowles
robknowles

About the only advice I can give is to check through your php.ini file and double check the session settings, check they are all correct and make sense, then make sure the folder specified to store the sessions files in a) exists and that the web server has the proper permissions to write to the folder.

Next thing is to use a simple couple of php pages (not the one's you're already using) just to check if sessions are working, e.g. have the page write something to the session, navigate to the second page and see if the sessions have indeed carried over.  If you find that they are working fine, then it's something connected with the php scripts you wrote for the windows system you were previously using, however if they don't work obviously it's still a session/php issue.

Hope that might have helped some, but you've probably already tried these anyways. cheers
No, not <br> - that's an HTML thing. Different OSs traditionally use different characters to denote ends of lines in plain text files. Windows uses a carriage return (ASCII 13, also known as 'CR') followed by a line break (ASCII 10, aka 'LF'), collectively known as CRLF. UNIX uses just LF, and just for completeness, MacOS uses just CR (though Mac OS X, as a UNIX platform, uses LF). Most modern text editors (that excludes notepad) can cope with multiple line break formats, but sometimes compilers and languages like PHP cannot. Generally it's a good idea to match your line breaks to the platform you're on.

As robknowles says, try a simple test to make sure it's the sessions themselves that are broken and not something else in your script:

<?php
session_start();
array_key_exists('test', $_SESSION) ? $_SESSION['test']++ : $_SESSION['test'] = 0;
print "test = {$_SESSION['test']}\n");
?>

If that works (should print a number that increments every time you hit it), then you know that sessions are not the problem.
Hi,

I tried Squinky's script and at the start I got some errors but eventually figured it out. :-)

The page prints out the numbers and increments everytime.

One more thing, where do I find the php.ini file in Linux?

Thanks!
Ah, sorry, there is a stray ) in it...

On linux php.ini usually in /etc/php.ini. phpinfo() will also tell you where it's looking.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Maybe I'm being a little stupid today, but I can't find what's wrong with the authentication code.

Here's checkLogin.php, and it just checks if the username and password are in the database...

---------

<?

if((!$_POST['username']) || (!$_POST['password'])) {
header("location: login.html");
}
else {

include 'include/connect.inc';

$sql = "SELECT * FROM users WHERE username = '{$_POST[username]}' AND password = '{$_POST[password]}'";
$result = @mysql_query($sql);
if (!$result) {
echo('<p>Error retrieving data:  ' . mysql_error() . '. <br /><br />I don\'t like this, do you?</p>');
exit;
}

$num = mysql_num_rows($result);
if ($num < 1) {
header("location: login.html");
exit;
}

session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
header("location: members_home.php");
}
?>

----------------

Now, I can get into member's home, but where the user's name is supposed to appear in the welcome section, is just a space. So it just shows "Welcome !" instead of "Welcome (username)!"

Then when I click on other links, there's the part where I get sent back to the login screen.

Is there something wrong with these lines below?

<?
session_start();

if((!$_SESSION['username']) || (!$_SESSION['password'])) {
header("location: login.html");
exit;
...rest of script...
?>

--------------
Hi,

Never mind guys. I decided to do something else instead. Quite unexpectable, but it worked.

In the login check script, the variables were still there but somehow in the members home page, it was gone. So I just made a new variable and set its value to the session variable. Everything works fine now.

Many thanks for your kind help!