Submit Your Article Forum Rules

Results 1 to 7 of 7

Thread: PHP Session control problem.

  1. #1
    Junior Member
    Join Date
    Jul 2004
    Posts
    5

    PHP Session control problem.

    Hi,

    I have been having some problems when working with PHP Session control under my site structure.
    My site structure acts as the following:

    To optimize the site, the main page (index.php) is the target for all the links
    of the pages inside the site. Even when sending form data from one page to another, the data (posted data) of the sending page is directed to index.php which propagates the information until it reaches the destination page.
    Since the index.php (which acts as a gateway) page is loaded (by the PHP interpreter) for any request inside the site, I have been having some problems with the PHP session control. Every time a request to index.php is made, another session is created even when I propagate the session ID (by url). By the way, when I try to use "session_id ( PROPAGATED_SESSION_ID )" telling to PHP that I do want to continue an already created session, the interpreter seems to crash sending no response.

    Could someone help me, please?

  2. #2
    Senior Member
    Join Date
    Apr 2004
    Posts
    446
    So let's have a look at the session code. All you usually need is session_start();, which will handle everything.

  3. #3
    Junior Member
    Join Date
    Jul 2004
    Posts
    5
    Quote Originally Posted by HardCoded
    So let's have a look at the session code. All you usually need is session_start();, which will handle everything.
    Hi,


    I have been done a test: The variable "VAR" has been set as a session variable which value should be reached in body.php. I have had no sucess with the following codes.

    Index.php:

    Code:
    <?php
    	
    	// re/initialize session
    	
    	if ( (!$_GET["PHPSESSID"]) && (!session_id()) )
    	{
    		session_start();
    	}
    	else
    	{
    		// this ID is sent back to index.php by other
    		// site page
    		session_id( $_GET["PHPSESSID"] );
    		session_start();
    	}
    	
    	// It is a test and I should get VAR value 
    	// in body.php.
    	//session_register( "VAR", "TESTE");
    	$_SESSION["VAR"]="TESTE";
    	
    	// propagate session ID to come back to index 
    	
    	// this ID is used for propagation
    	$sid = "PHPSESSID=" . session_id();
    	
    	//echo( $sid ) ;
    			
    ?>
    
    <!------------------------------------------------------------------------------
    	
    	RHER  
    	Consultoria e desenvolvimento
    	
    	RHER.com	v0.1
    	
    	index.php
    		
    	Pagina de indice do site
    	
    	
    	Data		Autor				Evento
    	26-01-2004  Rodrigo				Criacao
    	
    //----------------------------------------------------------------------------->
    
    <?php
            // requisicoes
            require( "./define.php" );
    		require( "./php/auth.php" );
    
            // obtencao dos parametros
            $body  = $_GET[ "body"   ];
    		$topic = $_GET[ "topic"  ];
    		$div   = $_GET[ "div"    ];
            $action= $_GET[ "action"    ];
    
    
    		
    		//
    		// dados de formulários
    		//
    		// os dados vindos pelo método POST 
    		// são armazenados nessa variável.
    		// Como o post do formulário chega 
    		// à página index.php, os dados são 
    		// repassados mediante o parâmetro 
    		// 'postdata' e método get.
    		// Como convencão, todos os dados de
    		// quaisquer que sejam os formulários 
    		// devem estar no array 'postdata'.
    		// Ex: para um input do form -> name='postdata[campo_01]'
    		//
    		$postdata = expandPostData($_POST["postdata"]);
    		
    		if ($body == '')
    			$body = "home";
    	
    		
    ?>
    
    
    
    <html>
    <head>
    <title>RHER Consultoria e desenvolvimento</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="description" content=".">
    <meta name="keywords" content="." >
    </head>
    	<link type="css/text" src="/css/common.css">
    	
    	<body bgcolor="#dfdfdf" >
    
    	<table height="100%" width="100%" valign=top align=center border=0 >
    	<tr><td valign=top>
    	
    	<div align=center valign=top>
    	<div class=conteudo align=center> 
    	
    	
    	<table height="400" width="770" valign=top align=center border=0  bgcolor="#dfdfdf" cellpadding=0 cellspacing=0>
    		<tr>
    			<td colspan=2> 
    				<table width="770" valign=top align=center border=0 cellpadding=0 cellspacing=0  bgcolor="#FFFFFF" >
    	
    					
    					<tr height=60 width=770>
    						<td align=center height=60 valign=middle colspan=2>
    							<?php include "$sUrl/header/header.php?action=show$sid" ?>
    						</td>
    					</tr>
    					
               			
    					<tr height=20 width=770>
    						<td align=center height=20 valign=middle colspan=2 width=770>
    							<?php include "$sUrl/menu/menu.php?action=show$sid" ?>
    						</td>
    					</tr>
    
    					<tr height=400 width=770>
    	
    						
    						<td align=center  valign=middle width=200 >
    							<?php include "$sUrl/side/side.php?$sid&body=$body&topic=$topic&div=$div&action=$action" ?>
    						</td>
    						
    						
    						<td align=center  valign=top width=570>
    							<?php include "$sUrl/body/body.php?$sid&body=$body&topic=$topic&div=$div&postdata=$postdata&action=$action" ?>
    						</td>
    						
    					</tr>
    					
    				</table>
    			</td>
    		</tr>
    		
    
    
    		 
    		<tr>
    			<td width=200 valign=top>[img]/images/footer_left.gif[/img]</td>
    			<td align=center height="26" valign=middle width=570>
    				<table valign=middle align=center border=0 cellpadding=0 cellspacing=0 width=570>
    					<tr>
    						<td bgcolor=#AEC1E7 width=550>
    							<?php include "$sUrl/footer/footer.php?action=show" ?>
    						</td>
    						<td valign=top width=20>[img]/images/footer_right.gif[/img]</td>
    					</tr>
    					
    				</table>
    			</td>
    		</tr>
    		
    	</table>
    	
    	
    	
    	</div>
    	</div>
    
    	</td></tr></table>
    
    <?php //echo("$body
    $topic
    $div
    $postdata"); ?>
    
    	</body>
    	
    </html>


    The body.php, a direct page called by index.php is:
    <?php

    // re/initialize session
    //session_id($_GET["PHPSESSID"]);
    //session_start();

    echo("
    BODY:".$_GET["PHPSESSID"]);
    echo("
    BODY:".session_id());

    // VAR value is set in index.php
    echo("
    BODY:".$_SESSION["VAR"]);


    ?>

    <?php
    // requisicoes
    require( "../define.php" );
    require( "../php/functions.php" );


    // obtencao dos parametros
    $body = $_GET[ "body" ];
    $topic = $_GET[ "topic" ];
    $action = $_GET[ "action" ];
    $div = $_GET[ "div" ];

    $postdata = urlencode( $_GET[ "postdata" ] );

    ?>


    <table width=570 height=400 valign=top align=center border=0 cellpadding=10 cellspacing=0>

    <!--<tr align=right width=500 valign=top>
    <td height=10></td>
    </tr>
    -->


    <tr align=right width=500 valign=top>
    <td height=20>
    <font class=data><?php data(); ?>.</font>
    </td>
    </tr>


    <tr align=center width=500 valign=top height=200>
    <td>
    <?php include "$sUrl/$body/$body.php?body=$body&topic=$topic&div=$div&action= $action&postdata=$postdata$sid" ?>
    </td>
    </tr>

    </table>

    <?php //echo("$body
    $topic
    $div
    $postdata"); ?>

  4. #4
    Senior Member
    Join Date
    Apr 2004
    Posts
    446
    You are way overcomplicating things. All you need is session_start(), which will take care of starting a new session only if one is not already going.

    So replace all this:
    Code:
      if ( (!$_GET["PHPSESSID"]) && (!session_id()) ) 
       { 
          session_start(); 
       } 
       else 
       { 
          // this ID is sent back to index.php by other 
          // site page 
          session_id( $_GET["PHPSESSID"] ); 
          session_start(); 
       }
    with this:

    Code:
    session_start();

  5. #5
    Junior Member
    Join Date
    Jul 2004
    Posts
    5

    You are right!

    Quote Originally Posted by HardCoded
    You are way overcomplicating things. All you need is session_start(), which will take care of starting a new session only if one is not already going.

    So replace all this:
    Code:
      if ( (!$_GET["PHPSESSID"]) && (!session_id()) ) 
       { 
          session_start(); 
       } 
       else 
       { 
          // this ID is sent back to index.php by other 
          // site page 
          session_id( $_GET["PHPSESSID"] ); 
          session_start(); 
       }
    with this:

    Code:
    session_start();

    Hi,

    You were right!

    I do have discovered my real problem:
    When I have to call another page, like body.php, I've been using something like this:

    include "http://127.0.0.2/body.php?action=1"

    Since I need to send a parameters to the included pages, I am forced to put the completed URL ("http://127.0.0.2/"), since I had had no success with

    include "body.php?action=1",

    With the entire URL the session data is not propagated to the target link.

    Now I have just one more question: Is there a way to which
    proapagate the entire session data when redirecting to
    another page outside the site "address space", as the way I had been using?



    Thanks a lot!

    Thank you!

  6. #6
    Senior Member
    Join Date
    Apr 2004
    Posts
    446
    The proper way to do this would be to simply
    include('body.php');

    Inside body.php, $_GET['action'] and $_SESSION will be available as globals.

  7. #7
    Junior Member
    Join Date
    Jul 2004
    Posts
    5
    Quote Originally Posted by HardCoded
    The proper way to do this would be to simply
    include('body.php');

    Inside body.php, $_GET['action'] and $_SESSION will be available as globals.
    Thanks a lot!!!!!!

Similar Threads

  1. Die SESSION, Die! (PHP Problem)
    By madmanlear in forum Web Programming Discussion Forum
    Replies: 2
    Last Post: 05-09-2007, 11:16 PM
  2. Session Ids
    By Fort Worth Realtor in forum Search Engine Optimization Forum
    Replies: 5
    Last Post: 01-12-2007, 11:42 PM
  3. session problem:plz help
    By Informer45 in forum Web Programming Discussion Forum
    Replies: 2
    Last Post: 12-17-2004, 07:21 AM
  4. Regarding session ids
    By site-report in forum Search Engine Optimization Forum
    Replies: 2
    Last Post: 03-16-2004, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •