Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Sessions

Status
Not open for further replies.

lazydays

Technical User
Sep 29, 2003
93
GB
Hi, I am having a bit of trouble getting my head rounf Session Variables at the moment.

I have the following code

<?php
session_start();

require_once ("../../mysql_connect_personal.php");

$query = "SELECT * FROM user_details WHERE user_id = '$user'";

$result = @mysql_query($query);

$item = mysql_fetch_array($result);

if ($item["user_id"]==$user) {
if ($item["user_password"]==$pass) {
$_SESSION["user_id"]=$item["user_id"];
$_SESSION['user_company_name']=$item["user_company_name"];
header("Location: . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/check_user.php");
}
}
?>

I get the old "Cannot add header information" warning.

Now, I may be being thick, but if I put the header information under the session_start() function, how will the variables be populated?
 
do you have some "print", "echo" or "printf" or whatever command that will echo something *BEFORE* the header?

you can't display nothing before a header(). May be some query is giving you some warning or error that display something (again) before the header.

cheers.
 
Nope - the code is as you see it.

The script is run from the "action" part of a form from a previous page.
 
header("Location: . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/check_user.php");

did you echo this line?

echo "Location: . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/check_user.php");

what do you get?
 
hmmm.. I'm blind like you, I don't see any error in the script.. the only guess I have is that you could have some error (not by script but for data i.e.) in:


require_once ("../../mysql_connect_personal.php");
** file doesn't exist or the path is incorrect

$result = @mysql_query($query);
$item = mysql_fetch_array($result);
** the query is giving you errors or $result has no rows or some mysql error. I would replace it with

$result = mysql_query($query)or die ("MySQL: " . mysql_error());

cheers.
 
Thanks for your help Chacalinc but still no dice!

Think I'll have a word with my ISP and make sure they haven't got any settings that are stopping me doing this.

It wouldn't suprise me actually as we had to change from asp to php as they wouldn't support our Ecom software on a Windows platform - because they didn't want to run Perl scripts...
 
wow! that's hard... to have to change a complete software only for an ISP change... hmm.. that's not good.

 
is it possible that the calling script is throwing up some code before the headers are sent? you can usually see whether this is the case from the source code of the failed page.

you could also try output buffering - before calling the script add a line
"ob_start();"
and after the script is called
"ob_end_flush();"

hth
Justin
 
Ok - after a lot of searching I have found a way that works - not sure whether it's a great solutions but so far it does stand up...

<?php
session_start();

require_once ("../../mysql_connect_personal.php");

$query = "SELECT * FROM user_details WHERE user_id = '$user'";

$result = mysql_query($query)or die ("MySQL: " . mysql_error());

$item = mysql_fetch_array($result);

if ($item["user_id"]==$user) {
if ($item["user_password"]==$pass) {
$_SESSION["user_id"]=$item["user_id"];
$_SESSION["user_company_name"]=$item["user_company_name"];
?>

<script type="text/javascript">
location.replace('check_user.php');
</script>

<?php

} else {
echo 'Password is incorrect.';
}
} else {
echo 'Username is incorrect.';
}

?>
 
if this works and the previous did not then it might be something to do with the session handling within your ISP's implementation of php session_handling.

glad you've found a solution though.
 
Something that might cause the 'cannot modify headers' problem is also whitespace above your <?php call. If you have a single space or carriage return there, the header function will fail.
 
I've seen a few things about that - there is no whitespace!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top