?
Solved

Moving PHP scripts from a Windows environment to Linux

Posted on 2005-04-06
9
Medium Priority
?
265 Views
Last Modified: 2006-11-18
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!
0
Comment
Question by:jericotolentino
  • 4
  • 4
9 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13714919
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.
0
 
LVL 5

Author Comment

by:jericotolentino
ID: 13715606
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...
0
 
LVL 1

Expert Comment

by:robknowles
ID: 13716892
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13718051
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.
0
 
LVL 5

Author Comment

by:jericotolentino
ID: 13722762
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!
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13724661
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.
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 300 total points
ID: 13724680
If that script is running ok, and numbers are incrementing, then sessions are definitely working and the problem must be elsewhere in your code. I'd check through your authentication code.
0
 
LVL 5

Author Comment

by:jericotolentino
ID: 13725647
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...
?>

--------------
0
 
LVL 5

Author Comment

by:jericotolentino
ID: 13749205
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question