FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Script to check if Session has started?





Diablosblizz
Hey I was wondering if there was a way to check if a session on a site has started, and if it has then to show a message.

Is this possible?
ncwdavid
In a php script "session_start()" should be the first thing after the "<?php" if you are using sessions on the site but using this should work:
Code:

if(session_start()){
echo "The session has started.";
}
else{
echo "The session has not started";
}

That should go at the very start of the page. Why do you want to know anyway if the session has start? Its a simple function that does not really fail alot.
Diablosblizz
Reason I want to check if the session is started, is because I am using a member system based on sessions, and I would like to only make some pages accessible to members logged in..


Also, this code does not work. Also, maybe this will help, I want the users to login on a iframe, then click on that link and be able to visit the page.
ncwdavid
Diablosblizz wrote:
Reason I want to check if the session is started, is because I am using a member system based on sessions, and I would like to only make some pages accessible to members logged in..


Also, this code does not work. Also, maybe this will help, I want the users to login on a iframe, then click on that link and be able to visit the page.


Well i tested it and it worked perfectly. What was the error? Can you show the code because that would be helpful.
Diablosblizz
http://hotelmario.info/ff.php

Use the account at the side of:

Username: Test
Pass: test


And on the left side click Poll, and it says In, then logout and try it again, and its still in.
ncwdavid
Yeah i see but why is it all done one the same page? if its eachoed in the middle of the page like that where do you have the "session_start()" positioned in the file? or do you have it like this:
Code:

if(session_start()){
$session_started =1;
}

//Then where it it displayed is it like this:
if($session_started == 1){
echo "The session has started.";
}
else{
echo "The seesion did not start.";
}


And when i said post the code i ment post the code in the file.
Diablosblizz
Is it possible to not have it on that page? Also, if that code was not for that page, it still does not work Sad
TheGustav
I have a user system that uses sessions... All I do at the top of every page is draw the username and admin variables from the session.

Then if I have a page that requires you to be logged in, it will check if the username variable is empty or not. /shrug
Diablosblizz
Can you post your code you did for that? Maybe I can alter the way you got yours?


Are you using Rmb-scripting.coms member system?
ncwdavid
This is a way of protecting your pages from non registered members with sessions:
Verify.php(Checks if the info submitted was right)
Code:

//this is after chekcing if the fields were filled and that stuff
$check = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$num = mysql_num_rows($check);
if($num<=0){
echo "The information you entered was not valid.";
}
else{
$userdata = mysql_fetch_array($check);
$_SESSION['userdata'] = $userdata;
header('Location: http://www.yourdomain.com/members/index.php');
}


Then at the top of everypage thats protected have this:
Code:

<?php
session_start();
$userdata = $_SESSION['userdata'];
if(!$userdata[username]){
header('Location: http://www.yourdomain.com/login.php');
exit();
}
else{
The rest of the page but the else statement is not really needed.
}

Hope that helped in some way.
Diablosblizz
Ahh finally I figured it out. Though, for some reason, I did this before, and it never worked, but I did include the config.php file.

Code:
<?php
include('members/config.php');

if($logged[username])
{
echo "HellO";
}else
{
echo "Hello";
}
?>



Also, your code did not work, but then again I did not know what to do.

Thank you for your support.
Related topics
Phoenix free PHP scripts
l killed myspace on my school's network!!!
Need password protected page paying - 60 FRIH$
Order forms and Chat rooms; How to? ?
Need Help Please (I offer all my frih$)
150 frih to make this SIMPLE script
Scripting DB to VSS: Question about Diffs
Emailing Username And Password
Images in MySQL
session not working behind router
best cms to match my need
Megaman Phoenix
.htaccess help
protect my administration pages.
User Authentication based Menu Panel
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.