开发者

Value of sessions lost while routing URL's

开发者 https://www.devze.com 2023-03-25 18:37 出处:网络
I have index.php file where all style开发者_如何学Csheets,js,etc files are included and i only change file in the content area of index.php using require once.Problem is that when user ask for some ot

I have index.php file where all style开发者_如何学Csheets,js,etc files are included and i only change file in the content area of index.php using require once.Problem is that when user ask for some other page sessions are lost....and session variable is undefined...this is my index.php...while i access main.php , I am getting session variable undefined error... ----index.php file-----

<?php session_start();?>
<?php require_once("cc_includes/sessions.php"); ?>
<?php require_once('cc_includes/functions.php'); ?>
<?php require_once("cc_includes/sanitize.php"); ?>
<?php require_once('cc_includes/route.php'); ?>
<?php require_once("cc_includes/mydb.php"); ?>
<?php
    if($request_uri_header!='')
    {
        require_once($request_uri_header);
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <?php require_once("cc_includes/default_files.php");?>
</head>
<title><?php echo $the_title;?></title>
<body id="page1">
<!-- header -->
    <div class="bg">
        <section>
            <?php require_once("cc_includes/message.php"); ?>
            <div class="main">
                <header>
                    <?php require_once("cc_includes/logo.php"); ?>
                    <?php require_once("cc_includes/navigation.php");?>
                    <?php require_once("cc_includes/slider.php"); ?>
                </header>
                <section id="content">
                    <div class="padding">
                        <?php require_once("cc_includes/boxes.php"); ?>
                        <div class="wrapper">
                            <div class="col-3">
                                <div class="indent">
                                    <?php
                                        if($request_uri!='')
                                        {
                                            require_once($request_uri);
                                        }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
                <?php require_once("cc_includes/footer.php"); 
                require_once("cc_includes/end_scripts.php"); 
                ?>
            </div>
        </section>
    </div>
</body>
</html>

-----sessions.php file------

$session_user=false;
    $session_message=false;
    if(!isset($_SESSION) || !isset($_SESSION['user']))
    {
        $session_user=array(
                's_name'=>'',
                's_gender'=>'',
                'college_id'=>'',
                's_joining'=>'Dynamic',
                's_department'=>'Dynamic',
                's_location'=>'',
                's_dob'=>'',
                's_approved'=>0
            );
        $_SESSION['user']=serialize($session_user);

    }
    else
    {
        //print_r(unserialize($_SESSION['user']));
        //exit;
        $session_user=unserialize($_SESSION['user']);
    }

-----route.php file--------

if(isset($_GET['url']))
    {
        $total_request=explode('/',$_GET['url']);
        if(count($total_request)>1)
        {
            $_GET['url']=$total_request[0];
            array_shift($total_request);
            $_GET['action']=$total_request[0];
            array_shift($total_request);
            foreach($total_request as $key=>$value)
            {
                $_GET['param'.$key]=$value;
            }
            unset($total_request);
        }
        if($session_user['s_approved']!=0)
        {
            if($_GET['url']=='' || $_GET['url']=='index.php')
            {
                header("location: main.php");
            }
            if(!is_file($_GET['url']))
            {
                set_error_message("No Such Location Exits!");
                header("location: main.php");
            }
            $request_uri=$_GET['url'];
            $request_uri_header="headers/".str_replace('.php','.h',$request_uri);

        }
        else
        {
            $request_uri="users/login.php";
            $request_uri_header=str_replace('.php','.h',$request_uri);
            if($_GET['url']!='' && $_GET['url']!='index.php')
            {
                if($_GET['url']=='services.php' || $_GET['url']=='register.php')
                {
                    $request_uri=$_GET['url'];
                    $request_uri_header="headers/".str_replace('.php','.h',$request_uri);
                }
                else
                {
                    if(is_file($_GET['url']))
                    {
                        set_error_message("You need to Login Before Accessing Other Site Area!");
                    }
                    else
                    {
                        set_error_message("No Such Location Exits!");
                    }
                    header("location: index.php");
                }
            }
        }
        if(!is_file($request_uri_header))
        {
            $request_uri_header='';
        }
    }
    else
    {
        $request_uri="users/login.php";
        $request_uri_header=str_replace('.php','.h',$request_uri);
    }

----main.h file-----

if(!registered_user() && !admin_user())
    {
        set_error_message("Please Login To View The Page!",2);
        header("Location: index.php");
    }
    set_title("College Connections | Home");
    view_boxes(false);
    view_slider(FALSE);

-----main.php file-----

<?php 
   // if(!isset($session_user))
    //{
        //echo $session_user['s_name'];
        //exit;
    //}
    //print_r($session_user);
    //exit;
    echo"<h1 class=\"profile\">".$session_user['s_name']."</h1><h1 class=\"blue_profile\">'s Profile</h1><a href=\"dashboard.php\" style='float:right;margin-right:30px;margin-top:20px;'>College Dashboard</a><br /></br /><br />";
    echo"<hr>";
    echo"<div class=\"prof_image\">";
    $c_id=$session_user['college_id'];
    $image=mysql_query("select path from img_upload where username=\"$c_id\" limit 1",$connection);
    if(!$image)
    {
        die("database query failed".mysql_error());
    }
    echo mysql_error($connection);
?>


You haven't called session_start() in any file other than index.php so when the other pages are loaded the sessions are being lost

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号