Jump to content

php function help


SN1P3R_85

Recommended Posts

hi, I am trying to make a function that will check a users status when viewing certain pages, to make sure they're logged on. If a user exists, then the variable $user will be true. So i made this function:

 

<?php

function user_valid()

{

if (!$user)

{

header('Location:http://www.mysite.com/access_denied.php');

}

}

?>

 

Then i made a test file to see if my function would work, and it didn't. I logged myself on, and then ran this code, and it said that I wasn't logged on, even though i was. here is the code:

 

<?php

include( '../user.inc' );          //contains my function, and is also where i declare $user.

 

user_valid();

 

echo "you're logged in!";

 

?>

 

It keeps saying im not logged on, even though i am. Can php functions be used like this?

Link to comment
https://forums.phpfreaks.com/topic/123090-php-function-help/
Share on other sites

hmm, why start a session variable? I'm just curious. If i do this it works:

 

<?php

include('../user.inc');

if ($user)

{

include( 'forum_menu.inc' );

}

else

{

header('Location:http://www.mysite.com/access_denied.php');

}

?>

 

its just when i put it in a function to try make it easier, it wont' work.

Link to comment
https://forums.phpfreaks.com/topic/123090-php-function-help/#findComment-635672
Share on other sites

example of using session variables:

login.php

<?php
session_start();
$_SESSION['user_name'] = "me";
$_SESSION['is_valid'] = true;
$_SESSION['user_level'] = "admin";
header("location:main.php");
?>

logout.php

<?php
session_start();
session_unset();
session_destroy();
header("location:main.php");
exit();
?>

main.php

<?php
session_start();
if (isset($_SESSION['is_valid']) && $_SESSION['is_valid'] == true){
print "<a href='logout.php'>Logout</a><br />You are logged in as ".$_SESSION['username']."<br />Userlevel: ".$_SESSION['user_level'];
}
else{
print "<a href='login.php'>Login</a>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/123090-php-function-help/#findComment-635684
Share on other sites

ok, im gonna post my entire user script so you can see how it works. I already have something along the lines of this, im just trying to make a nifty function i can put into anything i don't want non-users to see. Ok, here is the user script:

 

<?php

 

if ( array_key_exists( 'id', $_COOKIE ) && array_key_exists( 'pass', $_COOKIE ))        //this checks that id and password cookies are set

{

 

include( 'SQL_PASS.inc' );    //my sql login info

 

 

$sql_fetch = "SELECT * FROM login WHERE Id = '{$_COOKIE['id']}' AND Password = '{$_COOKIE['pass']}' LIMIT 1";

 

$lgn_result = mysql_query($sql_fetch);

 

if (!$lgn_result)

{

die('Could not run query: ' . mysql_error());

}

 

if (mysql_num_rows($lgn_result) == 0)

{

die('Wrong username or password, exiting');

}

 

 

$user = mysql_fetch_object( $lgn_result );

}

else

{

$user = false;

}

 

function user_valid()

{

if ($user) {}

else

{

header('Location:http://www.caidenhome.com/access_denied.php');

}

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/123090-php-function-help/#findComment-635686
Share on other sites

hmm, So when someone logs on i should make a session variable, and then just check for the session variable? Is there anyway to manipulate session variables, im worried about security. I would also have to make it so it checked if the cookie existed, and if it did, remake the session variable. I could do it that way if its better.

Link to comment
https://forums.phpfreaks.com/topic/123090-php-function-help/#findComment-635689
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.