This cookie is driving me madddddddddd

Discussion in 'PHP' started by jacka, Jul 24, 2008.

  1. #1
    Hi
    Please help me. I have been working on this for two days and it is just driving me mad.

    I have a simple connection to a table.
    When I put this file in one folder it works, when I transfer it in another folder it doesn't work.
    I have traced it down and I think it can't find the cookie from my computer.
    I can confirm that in both cases it connects to the database.
    I am testing locally. That is to say all files are in my computer except the database that are located on the remote server.
    Ok here we go : This works in the ORDER folder.
    <?php
     
    
    	include("dbi.php");
    		
    	
    	
    		$cxn5 = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die('Whoops! Could
    Not Connect to ODBC Database!');
    		
    		
    		$result3 = mysql_query("select * from icart where cookieId= '" . GetiCartId() . "' ");
    	
    			
    			
    	while ($row = mysql_fetch_array($result3))
    		{
    	
    	echo $row[qty];
    	echo "<br>";
    }// end of while
    
    
    ?>   
    Code (markup):
    But when I switch to another folder: it does not work.
    
    <?php
     
    
    	include("../orders/dbi.php");
    		
    	
    	
    		$cxn5 = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die('Whoops! Could
    Not Connect to ODBC Database!');
    		
    		
    		$result3 = mysql_query("select * from icart where cookieId= '" . GetiCartId() . "' ");
    	
    			
    			
    	while ($row = mysql_fetch_array($result3))
    		{
    	
    	echo $row[qty];
    	echo "<br>";
    }// end of while
    
    
    ?>   
    
    Code (markup):
    the only thing that has changed is the include file location.
    The first instance it is :
    include("../orders/dbi.php");
    Code (markup):
    : doesn't work

    The 2nd instance it is :
    include("dbi.php");
    Code (markup):
    : works

    There is nothing special in the order folder and I am not accessing anything in there.


    And in case you are wondering, this is the dbi script.
    
    
    <?php
    
    	// This page contains the connection routine for the
    	// database as well as getting the ID of the cart, etc
    
    	$dbServer = "aaa";
    	$dbUser = "bbb";
    	$dbPass = "ccc";
    	$dbName = "ddd";
    
    	function ConnectToDb($server, $user, $pass, $database)
    	{
    		// Connect to the database and return
    		// true/false depending on whether or
    		// not a connection could be made.
    		
    		$s = @mysql_connect($server, $user, $pass);
    		$d = @mysql_select_db($database, $s);
    		
    		if(!$s || !$d)
    			return false;
    		else
    			return true;
    	}
    	
    	function GetiCartId()
    	{
    		// This function will generate an encrypted string and
    		// will set it as a cookie using set_cookie. This will
    		// also be used as the cookieId field in the cart table
    		
    		if(isset($_COOKIE["icartId"]))
    		{
    			return $_COOKIE["icartId"];
    		}
    		else
    		{
    			// There is no cookie set. We will set the cookie
    			// and return the value of the users session ID
    			
    			session_start();
    			setcookie("icartId", session_id(), time() + ((3600 * 24) * 30));
    			return session_id();
    		}
    	}
    
    ?>
    
    Code (markup):
    Hope some one can shed a light at this.
    :confused::confused::confused::confused:
     
    jacka, Jul 24, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Based on that, try changing your dbi.php script's line.

    From:
    			setcookie("icartId", session_id(), time() + ((3600 * 24) * 30));
    PHP:
    To:
    			setcookie("icartId", session_id(), time() + ((3600 * 24) * 30), '/');
    PHP:
    I hope that works for you.

    Jay
     
    jayshah, Jul 24, 2008 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi
    Thanks for your time.
    But its a no go.
     
    jacka, Jul 24, 2008 IP
  4. lamborevijay

    lamborevijay Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think u can try by putting "session_start()" as the first line of the dbi.php file instead of putting it in "else".

    Thanx.
     
    lamborevijay, Jul 24, 2008 IP