I have made a site to make user interactive. I have like friends making site. But I am having problems. All the session variables gets destroyed after clicking some of the links. Do anybody have solution.
Go to
http://www.cgaclub.com.np/members/
See what is the problem. Please reply me.
I am desparately needing help..
well first of froma design point of view you shouldn`t make those menu parts be viewed when you are not logged in...like my profile and such...
Now as for the session stuff...you have to be sure to include a session_start() on top of every php page....make sure you don`t have a session_destroy() or a unset somewhere where it shoudn`t be... if you figure out which part of the code has problems...post it here and you will find help for sure
I have included a file in every page called session.php where session_start script is located. But also its not working, what may be the problem?
my guess is that you don`t have too much experience with sessions...so here is a very very brief tutorial...
When someone logges in you set some variables .. for example $_SESION['logged_in'] = 1.
Now on every page which should be user-only you must have something like..
| Code: |
<?php
session_start(); //You must put session start before any output is sent to the
//browser or else you will get an error
if($_SESION['logged_in']!=1)
{
// Option one..redirect the user to the login page
// Option two.. give an error message
//Option three...use your imagination
}
else
{
//show restricted page
}
//not quite indutry made, just for demonstration...
|
or something like this....see if you have these set up corectly, and if not try posting the code related to the sessions...from the log in page to the pages where the user must be logged on to view.
Cheers!
could you paste your sessions.php file here? as without any code it is impossible to say what goes wrong.
Here goes session.php codes
| Code: |
<?
session_start();
header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
if(!$_SESSION['username'])
{
session_unset();
session_destroy();
$url="Location: log.php";
header($url);
}
?> |
Hope i will get the solution. I searched Php manual and found out many had problem with variable getting destroyed. Still unsure what the hell is the problem.
Now you check for the boolean-value of $_SESSION['username'], and not if it is set (as you in fact want to do).
try
| Code: |
| !isset($_SESSION['username']) && $_SESSION['username'] != "" |
instead of !$_SESSION['username'].
I think this is you solution...
Edit: and place "exit;" after the header-function. This makes sure nothing can be executed after you redirected.
Greatz,
BlackSkad
No the problem was not solved. I said not all clicks on the links delete the session variables, just a link that point to search epals does that. Any solution?
Am I being stupid or is that a weird header thing? Also don't you want to set $_SESSION['username'] before checking if it's set?
What the most common reason for session destruction is the fact that session_start() is not the first thing on a page. Be sure that there are no whitespaces or no other form of text before session_start(), especially no HTML headers.
In general your code should look like:
<php
session_name(optional)
session_start()
?>
<HTML>....rest goes here....</HTML>