Computing.Net > Forums > Web Development > PHP session related prob

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP session related prob

Reply to Message Icon

Name: robber2
Date: October 29, 2007 at 09:59:51 Pacific
OS: w xp home
CPU/Ram: 2 gig celeron, 256 MB
Product: dell dim 2350
Comment:

When this page loads, I get the warnings, and I don't understand them. Anyone care to explain?

Page (view source for HTML):
http://perfectionconstruction.com/

PHP code:

session_start();
if(!session_is_registered('counted')){
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('l F j, Y - g:i a');
$to = "robazar@earthlink.net";
if($ref == ""){
$ref = "None";
}

$entry = "$dtime \n IP: $ip \n Agent: $agent \n URL: $uri \n Referrer: $ref \n\n";
$fp = fopen("goodlog.txt", "a");
fputs($fp, $entry);
fclose($fp);
session_register('counted');
}



Sponsored Link
Ads by Google

Response Number 1
Name: robber2
Date: October 29, 2007 at 10:26:46 Pacific
Reply:

I just read this. Is this what causes the warnings?
"Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent."


0

Response Number 2
Name: robber2
Date: October 29, 2007 at 12:30:32 Pacific
Reply:

(Forgive me while I talk to myself here!)
I added a couple lines to the PHP and the warnings went away. I don't understand it all but...

Added:

$user = $_SERVER['PHP_AUTH_USER'];

and:

if($user == ""){
$user = "None";
}

$entry = "$dtime \n IP: $ip \n Agent: $agent \n URL: $uri \n Referrer: $ref \n $user \n\n";

NOW the prob is that when I try to refresh the page w/ I.E.6, it gives another error, and the page doesn't load. If I try a "back to home" link on one of the other pages, same error. I.E.7 and Foxfire don't do that, but all of them post a new log each time the page is accessed or refreshed...(? is that a new session).
Hurting my brain here, but I'm not gonna quit!


0

Response Number 3
Name: Michael J (by mjdamato)
Date: October 29, 2007 at 16:00:47 Pacific
Reply:

I would suggest commenting all of your session specific code and uncomment the code line-by-line to see where the error exists.

But I "think" I see the error. The commond session_register() is deprecated and you don't seem to be using it correctly anyway. According to the manual the string inside the command should represent a global variable name. I don't see where you have defined $counted. ( http://us.php.net/manual/en/functio... )

Try this:

Change this:
if(!session_is_registered('counted')){
To This:
if($_SESSION['counted']!==true){


And change this:
session_register('counted');
To this:
$_SESSION['counted'] = true;


Michael J


0

Response Number 4
Name: robber2
Date: October 29, 2007 at 20:57:38 Pacific
Reply:

I made the changes and it works much better. Thanks.
'cept, tho IE6 logs 1 visit no matter how many refreshes, Foxfire counts each refresh and logs it. Can't figure that one.
Also, launch the page and refresh with IE6 and you get the 500 error. That also happens when you click the "home" link on any of my other pages.


0

Response Number 5
Name: robber2
Date: October 29, 2007 at 22:02:06 Pacific
Reply:

(talking to myself again...)
"Foxfire counts each refresh and logs it."
That's 'cause FF wasn't setting a cookie. When I allowed it (IE must've been allowed already), then:
"you get the 500 error"
To correct that, I moved a bit if the PHP code from one place to another, the "$-SESSION['counted'] =true" bit.
Two versions included below, the first barked the 500 error, the second flies. Don't ask!

(not good:)

session_start();
if($_SESSION['counted']!== true){
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$user = $_SERVER['PHP_AUTH_USER'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('l F j, Y - g:i a');
$to = "robazar@earthlink.net";
if($ref == ""){
$ref = "None";
}
if($user == ""){
$user = "None";
}

$entry = "$dtime \n IP: $ip \n Agent: $agent \n URL: $uri \n Referrer: $ref \n $user \n\n";
$fp = fopen("goodlog.txt", "a");
fputs($fp, $entry);
fclose($fp);
$_SESSION['counted'] = true;

$message = stripslashes("IP: $ip - DATE: $dtime" . "<br />" . $agent . "<br />" . "Ref\'d by: $ref");
$subject = ("A visit to your website");

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: PerfectionWebsite-";

mail($to, $subject,
$message, $headers);

}

(better:)

session_start();
if($_SESSION['counted']!== true){
$agent = $_SERVER['HTTP_USER_AGENT'];
$uri = $_SERVER['REQUEST_URI'];
$user = $_SERVER['PHP_AUTH_USER'];
$ip = $_SERVER['REMOTE_ADDR'];
$ref = $_SERVER['HTTP_REFERER'];
$dtime = date('l F j, Y - g:i a');
$to = "robazar@earthlink.net";
if($ref == ""){
$ref = "None";
}
if($user == ""){
$user = "None";
}

$entry = "$dtime \n IP: $ip \n Agent: $agent \n URL: $uri \n Referrer: $ref \n $user \n\n";
$fp = fopen("goodlog.txt", "a");
fputs($fp, $entry);
fclose($fp);

$message = stripslashes("IP: $ip - DATE: $dtime" . "<br />" . $agent . "<br />" . "Ref\'d by: $ref");
$subject = ("A visit to your website");

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: PerfectionWebsite-";

mail($to, $subject,
$message, $headers);
$_SESSION['counted'] = true;
}


0

Related Posts

See More



Response Number 6
Name: robber2
Date: October 30, 2007 at 20:55:59 Pacific
Reply:

I meant Firefox. I'm dyslexic and spell bad.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP session related prob

PHP Session Timeout www.computing.net/answers/webdevel/php-session-timeout/3877.html

PHP sessions not working www.computing.net/answers/webdevel/php-sessions-not-working/571.html

PHP session problem www.computing.net/answers/webdevel/php-session-problem/3516.html