...

View Full Version : Session handling



bahy
04-14-2003, 11:01 PM
How do i add link to an account in mydatabase by sesion ?
Like this : I have a login form and after user login i'll check if the user is admin or not , if admin then go to admin page by using javascript function window.location = "admin.php???????????????" the ??????????? can be the sesion or not ( i don't know yet ) . But i did try using window.location = <? echo "admin.php".session_id() ?> but no use . I did put the session_start() at the top of my page .
When i set window.location = "admin.php" it does not know the username of the admin .It just display HTML , that is without any variable i want it to display from the database . How do i solve this ? please help .

missing-score
04-14-2003, 11:22 PM
if you put

"admin.php".session_id();

it probably wont work, as you dont have the ? mark, or the variable.

Try




<?
$SESSID = session_id();
echo "admin.php?sessionid=".$SESSID;
?>



Hope that works for you :thumbsup:

Nightfire
04-15-2003, 01:30 AM
Well before you validate ppl, you'd select which user is admin, so maybe you'd create the $_SESSION['admin']. All you'd have to do is



if($_SESSION['admin']){
include("adminpage.php");
}else{
if($_SESSION['normaluser']){
include("normaluser.php");
}else{
echo "I ain't goin on no plane foo'!";
}
}

bahy
04-15-2003, 04:16 PM
Are u serious ? I am using php with javascript , when i the tag <script>
i put Var target = <? ----phpcode----?> it return nothing , but when i don't use <?----- ?> it works normally , How can this be solve ?
i want the page to direct to another page and also using some variable of the first page .
I am telling this again because i don't understand the var $_session , is there any clearer doc for me , plz tell me where is that document .

missing-score
04-15-2003, 04:37 PM
Are you sure you have short tags enabled. Try using

<?php

and then and then your code.

bahy
04-17-2003, 09:08 AM
it is foe sure that my system has the short tag enabled . I coded in sgort tag all the time and there is no problem until i used it with the javascript as i told above .

missing-score
04-17-2003, 11:11 AM
It seems like it should work. Can you post the code please.

If not, you could try getting PHP to echo the script.

stars75238
04-18-2003, 08:30 PM
I just completed an authentication system, where after a user logins in it assigns an access level and that is used in a switch statement. The switch statement allows me to load what page I want as an include and then exits teh script.

echo "Your Access Level is <strong>". $_SESSION['user_level']."</strong> This allows you the following options: <br /><br />";
switch($_SESSION['user_level']){
case "255":
include '255.php'; // Access 255!
mysql_free_result($manufacturer);
exit();
case "1":
include '1.php'; // Access 1!
mysql_free_result($manufacturer);
exit();
default:
include '1.php'; // Access 1!
mysql_free_result($manufacturer);
exit();
}
?>

bahy
04-19-2003, 08:50 PM
Thanks all men !
Actually i have another way to solve this now , but this is still something interesting to discuss on . I solved this with the session handles too , but not using the way of sending the session ID to the url , just register the var and use it again .

If u want my code then i'll post it below , don't care abot the other file i am using , it just a class for display in the HTML , and the words with strange character are the UNICODE words of my language ,Vietnamese .


<?
session_start();
require("functions.php");
require("page.inc");

$error1 = "";

$login = new page("Trang đăng nhập ");

if($submit)
{
$db=mysql_connect("localhost","admin","password");
mysql_select_db("test",$db);

$result1 = mysql_query("SELECT username FROM user WHERE username='$username'");
#kiem tra ket qua truy van

# Gap loi
if(!$result1)
{
$error1 = "<font color = red size=4>Query error </font>";
}
# THANH CONG
else
{
#Ton tai user

if (mysql_fetch_row($result1)>0)
{
$result2 = mysql_query("SELECT username,password FROM user WHERE username='$username' AND password=password('$password')");

#Kiem tra truy van lan` 2
if(!$result2)
{

$error1 = $error1."<font color = red size=4>Query error </font>";
}
#THANH CONG
else
{
# SAI PASSWORD
$result2 = mysql_query("SELECT username,password FROM user WHERE username='$username' AND password=password('$password')");

if(mysql_fetch_row($result2)==0)
{

$error1 = $error1."<br><font color=red size=4>Sai password </font> ";

$login->Displayabove_Menu();
$login->DisplayMenu($this->buttons);
$login->DisplaybelowMenu();
$login->content2 = $error1."<br><p> Bạn h&#227;y đăng nhập với t&#234;n v&#224; mật khẩu đ&#227; đăng k&#253; trước đ&#243; . Nếu chưa đăng k&#253; th&#236; bạn h&#227;y <a href =\"register.php\"><font color=red>Ấn v&#224;o đ&#226;y </font></a>."
."<br><form method=\"post\" action =\"<? echo $PHP_SELF ; ?>\" >"
."<p>"
."T&#234;n truy cập :<input type=text name=username size=20><br>"
."Mật khẩu &nbsp;&nbsp;&nbsp;&nbsp;:<input type=password name=password size=20><br>"
."<p>"
."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
."<input type=submit name=submit value=\"Đăng nhập\" >";

$login->DiplayContent("Trang đăng nhập ","$this->content2");
$login->Display_Right();

}

#username va password DUNG
else
{
$valid_user = $username;
session_register("valid_user");
?>
<form name="redirect">
<center>
<table border="5" bgcolor="#6B9AAD">
<tr>
<td>
<b><font size =6>T&#224;i khoản của bạn đ&#227; hợp lệ </FONT>
<br> Bạn sẽ được đưa đến quản l&#253; của c&#225;c th&#224;nh vi&#234;n trong v&#242;ng </b><input type="text" size="3" name="redirect2"> gi&#226;y
<br/><br/>Nếu bạn kh&#244;ng muốn đợi th&#236; h&#227;y <a href="member.php" title="Trang d&#224;nh cho c&#225;c th&#224;nh vi&#234;n !">ấn v&#224;o đ&#226;y </a>.
</td>
</tr>
</TABLE>

</center>
</form>

<script>
<!--

var targetURL="member.php"
var countdownfrom=5

var currentsecond=document.redirect.redirect2.value=countdownfrom+1
function countredirect(){
if (currentsecond!=1){
currentsecond-=1
document.redirect.redirect2.value=currentsecond
}
else{
window.location=targetURL
return
}
setTimeout("countredirect()",1000)
}

countredirect()
//-->
</script>
<?

}
}
}


else #khong ton tai user
{
$error1 = $error1."<font color = red size=4>Kh&#244;ng tồn tại t&#224;i khoản c&#243; t&#234;n truy cập l&#224; $username</font>";
$login->Displayabove_Menu();
$login->DisplayMenu($this->buttons);
$login->DisplaybelowMenu();
$login->content2 = $error1."<br><p> Bạn h&#227;y đăng nhập với t&#234;n v&#224; mật khẩu đ&#227; đăng k&#253; trước đ&#243; . Nếu chưa đăng k&#253; th&#236; bạn h&#227;y <a href =\"register.php\"><font color=red>Ấn v&#224;o đ&#226;y </font></a>."
."<br><form method=\"post\" action =\"<? echo $PHP_SELF ; ?>\" >"
."<p>"
."T&#234;n truy cập :<input type=text name=username size=20><br>"
."Mật khẩu &nbsp;&nbsp;&nbsp;&nbsp;:<input type=password name=password size=20><br>"
."<p>"
."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
."<input type=submit name=submit value=\"Đăng nhập\" >";

$login->DiplayContent("Trang đăng nhập ","$this->content2");
$login->Display_Right();

}
}

}
#CHo hien trang chinh
else
{
$login->Displayabove_Menu();
$login->DisplayMenu($this->buttons);
$login->DisplaybelowMenu();
$login->content2 = "<p> Bạn h&#227;y đăng nhập với t&#234;n v&#224; mật khẩu đ&#227; đăng k&#253; trước đ&#243; . Nếu chưa đăng k&#253; th&#236; bạn h&#227;y <a href =\"register.php\"><font color=red>Ấn v&#224;o đ&#226;y </font></a>."
."<br><form method=\"post\" action =\"<? echo $PHP_SELF ; ?>\" >"
."<p>"
."T&#234;n truy cập :<input type=text name=username size=20><br>"
."Mật khẩu &nbsp;&nbsp;&nbsp;&nbsp;:<input type=password name=password size=20><br>"
."<p>"
."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
."<input type=submit name=submit value=\"Đăng nhập\" >";

$login->DiplayContent("Trang đăng nhập ","$this->content2");
$login->Display_Right();

}

?>




the javascript send the user to member.php , in this file i will check the session registered var and use it later



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum