PDA

View Full Version : کمک در مورد session و ریدایرکت **فوری***



Master_Power
سه شنبه 23 تیر 1394, 18:31 عصر
سلام
یه فرم ورود دارم که همه چیزش کار میکنه ولی وقتی که آدرس پنل کاربر را مستقیم تایپ میکنم میره تو پنل و تمام رکورد های بانک را نشون میده

مشکل از session هست که وقتی ساخته نشده صفحه را به فرم ورود ریدارکت نمیکنه و جلوگیری از ورود به پنل نمیکنه

ولی موندم چجوری بنویسم که این مشکل حل بشه
لطفا کمک کنید
این کدهای صفحه فرم ورود هست index.php

<?php include(dirname(__FILE__).'/../headers.php');?>
<div class="row" style="background-color:#f1f1f1; border-radius:5px; border:1px solid #999999; margin-bottom:2%; padding:1.5%;">
<script type="text/javascript" src="<?php echo url; ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo url; ?>/js/login.js"></script>
<div class="col-lg-4 col-lg-offset-4">
<div class="panel panel-default">
<div class="panel-heading" style="text-align:center">ورود به پنل کاربری</div>
<div class="panel-body">
<img src="img/photo_login.png" class="img-circle img-responsive center-block" style="margin-bottom:5%">
<form method="post" action="" id="login-form">
<!-- %%%%%%%%%%%%%%%%%%%%% -->
<div class="row">
<div class="form-group">
<div class="col-lg-12" style="margin-bottom:2%">
<input type="text" name="username" placeholder="نام کاربری" id="username" class="user-field form-control">
</div>
</div>
</div>
<!-- %%%%%%%%%%%%%%%%%%%%% -->
<!-- %%%%%%%%%%%%%%%%%%%%% -->
<div class="row">
<div class="form-group">
<div class="col-lg-12" style="margin-bottom:2%">
<input type="text" name="password" id="password" placeholder="پسورد" class="pass-field form-control">
</div>
</div>
</div>
<!-- %%%%%%%%%%%%%%%%%%%%% -->

<input type="button" value="ورود" class="login_btn btn btn-success btn-block"/>
<div class="loading" style="text-align:center">
<img src="files/icons/loading.gif"/>
</div><br>
<div id="message_login" style="text-align:center">

</div>

</form></div> <div class="panel-footer" style="text-align:center"><a href="<?php echo url; ?>/submit">برای عضویت اینجا کلیک کنید</a></div>
</div>
</div>

</div><!-- // row -->

<?php include(dirname(__FILE__).'/../footer.php');?>

اینم کد قسمت خوندن از بانک و ساخت session هست login.php

<?php
session_start();
require('../config.php');
require('include/function.php');

if(isset($_POST['username']) and (isset($_POST['password'])))
{
$username=$_POST['username'];
$username=stripslashes($username);
$username= mysql_real_escape_string($username);
$password=$_POST['password'];
$password=stripslashes($password);
$password=mysql_real_escape_string($password);

$sql="SELECT kj_username,kj_password FROM kj_info WHERE kj_username='".$username."' and kj_password='".$password."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

if($result and mysql_num_rows($result)>0)
{


display_message ('yes');
$_SESSION['user']=$username;
}
else
{
display_message ('Accsess Denies');
header('location:index.php');
}
}
?>
خب وقتی که کاربر وارد میشه، میره تو صفحه panel.php که اطلاعاتش هست

ولی مشکل اینجاست که وقتی ما آدرس را مستقیم اینجوری مینویسیم http://localhost/acount/panel.php هم باز میره
در صورتی که باید با دستور location که گذاشتم ریدایرکت کنه رو صفحه فرم ورود index.php که نمیشه

لطفا راهنمایی کنید
راستی اینم فایل function.php

<?php
/********************************/
function display_message($txt)
{
echo $txt;
}
/********************************/
function redire()
{
header('location:index.php');
}
/*******************************/
function check_user()
{
if(!isset($_SESSION['user']))
{
redire();
}
}
/********************************/
function filter_input_txt($txt_input)
{
stripslashes($txt_input);
mysql_real_escape_string($txt_input);
}

?>

mohammad.cs
پنج شنبه 25 تیر 1394, 13:22 عصر
با سلام

کدت خیلی مشکل دارد:
1-اینطور که من میبینم شما اصلا فقط از یکی از این توابعت استفاده کرده ای و مابقی دکوری اند.
2-سشن را در هر صفحه ای که استفاده میکنی باید ابتدا استارتش کنی.
3-اکشن فرم اندکس را تعیین نکرده ای که به کدام صفحه برود.
4-در صفحه لوگین تعیین نکرده ای که پس از زدن دکمه کدها کار کنند و اینکه نام و نوع دکمه را اشتباه انتخاب کرده ای.
5-پس از هیدر باید exit() کنید-برای امنیتش.
6-اگر هم از PDO استفاده کنید خیلی بهتر میباشد.

موفق باشید


یک مثال:





<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/css/bootstrap.min.css" rel="stylesheet">
<link href="style/css/bootstrap-rtl.css" rel="stylesheet">
</head>
<body>

<?php
session_start();
include("connect.php");


if(isset($_POST["submit"]))
{
if($_POST["user"]!="" and $_POST["password"]!="" and $_POST["captcha"]!="")
{
if($_POST["captcha"]!=$_POST["hid"])
{
header("location:login.php");
exit;
}
else
{
$q="select * from users where username=? and password=?";
$result=$connectt->prepare($q);
$userzh=mysql_real_escape_string($_POST["user"]);
$passzh=mysql_real_escape_string($_POST["password"]);
$result->bindvalue(1,$userzh);
$result->bindvalue(2,$passzh);
$result->execute();
$num=$result->fetchColumn();
if($num>0)
{
$_SESSION["o"]=20;
header("location:admin/admin.php");
exit();
}/*if*/
else
{
header("location:login.php");
exit();
}/*else*/

}/*captcha*/


}/*if notempty*/
else
{
echo "<font dir=rtl color=#E93437 size=+6 >حداقل یکی از فیلدها خالی میباشد</font>";
}/*empty*/
}/*if*/

?>


<section class="container">

<article class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form name="form1" method="post" action="hamin safeh.php">
<table class="table table-condensed">
<tbody>

<tr>
<td><input name="user" class="form-control" required="" type="text" id="user" placeholder="نام کاربری " >
</td>
</tr>
<tr>
<td><input name="password" class="form-control" required="" type="password" id="password" placeholder="رمز عبور ">
</td>
</tr>
<tr>
<td><input name="submit" type="submit" id="submit" class="btn btn-block btn-danger active" value="ارسال"></td>
</tr>
</tbody>
</table>
</form>
</article>
<?php
include("footer.php");
?>