MoMoMajor Posted October 17, 2008 Share Posted October 17, 2008 <?php session_start(); if(isset($_SESSION['views'])) { $_SESSION['views'] = $_SESSION['views']+ 1; } else { $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; } ?> When I run the above script I get this error : Fatal error: Unsupported operand types in C:\xampp\htdocs\Website\sessions.php on line 4 I don't know why this is happening especially since I just copied and pasted this code from tizag.com php tutorials. I did modify the code slightly ... it was originally: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; ?> I just added some {} around the if statements. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 17, 2008 Share Posted October 17, 2008 Try this instead and let me know... if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 17, 2008 Share Posted October 17, 2008 P.S - I've seen this error before by the way, what version of PHP are you running? Echoing phpinfo() will tell you this. Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted October 17, 2008 Author Share Posted October 17, 2008 Try this instead and let me know... if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } When I run the script all I get is a blank page. P.S - I've seen this error before by the way, what version of PHP are you running? Echoing phpinfo() will tell you this. I am running 5.2.6 Quote Link to comment Share on other sites More sharing options...
waynew Posted October 17, 2008 Share Posted October 17, 2008 If it's blank it could mean: a) That the session variable "views" isn't set or that b) There's an error but you don't have error reporting turned on. Try error_reporting(E_ALL); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 17, 2008 Share Posted October 17, 2008 Well that means it's probably fixed. You just need to add in... echo "views = ". $_SESSION['views']; To view the session views variable. Your final script should look like... <?php session_start(); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } echo "views = ". $_SESSION['views']; ?> Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 17, 2008 Share Posted October 17, 2008 If it's blank it could mean: a) That the session variable "views" isn't set or that b) There's an error but you don't have error reporting turned on. Try error_reporting(E_ALL); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } I think he just copied my code directly rather than integrating it into his own script and I hadn't included the echo command which is why he didn't see anything. Quote Link to comment Share on other sites More sharing options...
waynew Posted October 17, 2008 Share Posted October 17, 2008 As GKWelding pointed out in his script. You MUST use session_start(); At the top of every page when you're dealing with sessions. Quote Link to comment Share on other sites More sharing options...
waynew Posted October 17, 2008 Share Posted October 17, 2008 <?php session_start(); error_reporting(E_ALL); //hell yea motherfucker if(isset($_SESSION['views'])){ $_SESSION['views'] ++; } else{ $_SESSION['views'] = 1; } echo "views = ". $_SESSION['views']; ?> Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted October 17, 2008 Author Share Posted October 17, 2008 If it's blank it could mean: a) That the session variable "views" isn't set or that b) There's an error but you don't have error reporting turned on. Try error_reporting(E_ALL); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } Still blank. I get a lot of error messages so that would mean they are turned on right? Or is their more then one setting? Well that means it's probably fixed. You just need to add in... echo "views = ". $_SESSION['views']; To view the session views variable. Your final script should look like... <?php session_start(); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } echo "views = ". $_SESSION['views']; ?> Return is : CRAP!views = Array Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted October 17, 2008 Author Share Posted October 17, 2008 Oh wait ... I did just c/p into the code. I just added session_start() <?php session_start(); error_reporting(E_ALL); if (isset($_SESSION['views'])) { if (!is_numeric($_SESSION['views'])) { echo "CRAP!"; } ++$_SESSION['views']; }else{ $_SESSION['views'] = 1; } ?> it returns: CRAP! Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 17, 2008 Share Posted October 17, 2008 In that case your view is an array and not an integer which is why you were getting the operand error in the first place. You probably need to clear out the session and try rerunning the script as you've probably still got the old session open. Quote Link to comment Share on other sites More sharing options...
waynew Posted October 17, 2008 Share Posted October 17, 2008 You session_destroy(); ONCE. Then remove it from your script. Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted October 17, 2008 Author Share Posted October 17, 2008 ok it works now thank you very much guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.