11 Replies - 2346 Views - Last Post: 11 May 2010 - 07:57 AM Rate Topic: -----

#1 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

trouble with arrays and $_SESSION

Posted 05 May 2010 - 09:19 AM

Hi

I'm learning php and my latest project is to create a script that uses session functions to remember which pages in my environment the user has visited. Provide the user with a list of links on each page to make it easy for the user to retrace his/her steps. I've completed the main page and the second page, but it appears my problem is with the main page. It displays fine, but when I click on page 2 I get Forbidden

Quote

You don't have permission to access /<br /><b>Warning</b>: array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument on this server.
My code is:

<?php
	session_start();
?>
<html>
<body>
<?php
if (empty($_SESSION['sites'])) 
	{
		$_SESSION['sites']="http://localhost/session1.php";
	}
function linkage($link)
	{
		$vlinks['addsite']=$link;
		$sites=array_unique(array_merge(unserialize($_SESSION['sites']),$vlinks['addsite']));
		$_SESSION['sites'] =serialize($sites);
		return $link;
	}
?>
<center><h1>This is the main page</h1></center>
<br><br>
<p> visit one of the following pages</p>
<a href="<?php echo linkage("http://localhost/session2.php"); ?>">Page 2 </a><br>
<a href="<?php echo linkage("http://localhost/session3.php"); ?>">Page 3 </a><br>
<a href="<?php echo linkage("http://localhost/session4.php"); ?>">Page 4 </a><br>
<a href="<?php echo linkage("http://localhost/session5.php"); ?>">Page 5 </a><br>
</body>



I know that I probably should use includes but the book hasn't covered that yet so I'm deliberately not using it.
Here is Page 2 just in case you need it:
<?php
	session_start();
?>
<html>
<body>
<center><h1>This is the Second Page</h1></center>
<br><br><br><br><br><br><br><br><br>
<h3>Pages already visited</h3>
<?php
	function linkage($link)
		{
		vlink['addsite']=$link;
		$site=array_unique(array_merge(unserialize($_SESSION['sites']),$vlink['addsite']));
		$_SESSION['sites'] =serialize($site);
		return $link;
		}
	?>
<ol>
<?php
	foreach (unserializ($_SESSION['sites']) as $s)
		{
		switch ($s)
			{
			case "http://localhost/session1.php":
				$name="Main Page";
				break;
			case "http://localhost/session2.php":
					$name="Page2
					break;
			case "http://localhost/session3.php":
				$name="Page 3";
				break;
			case "http://localhost/session4.php":
				$name="Page 4";
				break;
			case "http://localhost/session5.php":
				$name="Page 5";
				break;
			}
		if ($name="Page 2") {continue;}	
		echo "<li><a href=\"".linkage($s)."\">".$name."</a>";
		}
?>
</ol>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: trouble with arrays and $_SESSION

#2 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: trouble with arrays and $_SESSION

Posted 05 May 2010 - 10:03 AM

I don't think include is the right thing for the job here. Why don't you try array_push() for adding elements to the end of an array instead of serializing and unserializing everything?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: trouble with arrays and $_SESSION

Posted 05 May 2010 - 10:11 AM

Typo:
foreach (unserializ($_SESSION['sites']) as $s)


Missing quote:
$name="Page2
                                        break;


Assignment, not comparison:
if ($name="Page 2") {continue;} 

Was This Post Helpful? 1
  • +
  • -

#4 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 05 May 2010 - 11:39 AM

View PostJackOfAllTrades, on 05 May 2010 - 10:11 AM, said:

Typo:
foreach (unserializ($_SESSION['sites']) as $s)


Missing quote:
$name="Page2
                                        break;


Assignment, not comparison:
if ($name="Page 2") {continue;} 

OMG how could I overlook the assignment instead of comparision. Completely forgot about that syntax. THANKS. so is it page 2 giving me that error?
Was This Post Helpful? 0
  • +
  • -

#5 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 06 May 2010 - 08:38 AM

thanks jrm402. I did as you recommended, but I still am getting a weird error which now mentions the array_push. The error I get is:

Quote

Forbidden

You don't have permission to access /<br /><b>Warning</b>: array_push() [<a href='function.array-push'>function.array-push</a>]: First argument should be an array in <b>C:/httpdocs/session1.php</b> on line <b>13</b><br /><br /><b>Warning</b>: array_unique() [<a href='function.array-unique'>function.array-unique</a>]: The argument should be an array in <b>C:/httpdocs/session1.php</b> on line <b>13</b><br />http://localhost/session2.php on this server.


What I don't understand is why his message is appearing. Am I not allowed to have a php code within the href= or is it more that I cannot call a function inside there. Logically as far as I can tell this should work, but obviously something illegal is happening here. Please help. The error I get happens only after I click on a link.




<?php
	session_start();
?>
<html>
<body>
<?php
if (empty($_SESSION['sites'])) 
	{
		$_SESSION['sites']="http://localhost/session1.php";
	}
function linkage($link)
	{
		$vlink=array_unique(array_push($_SESSION['sites'],$link);
		$_SESSION['sites'] =$sites;
		return $link;
	}
?>
<center><h1>This is the main page</h1></center>
<br><br>
<p> visit one of the following pages</p>
<a href="<?php echo linkage("http://localhost/session2.php"); ?>">Page 2 </a><br>
<a href="<?php echo linkage("http://localhost/session3.php"); ?>">Page 3 </a><br>
<a href="<?php echo linkage("http://localhost/session4.php"); ?>">Page 4 </a><br>
<a href="<?php echo linkage("http://localhost/session5.php"); ?>">Page 5 </a><br>
</body>
</html>



Oops I found an error in the code but I still get that message. Here is the code again:
<?php
	session_start();
?>
<html>
<body>
<?php
if (empty($_SESSION['sites'])) 
	{
		$_SESSION['sites']="http://localhost/session1.php";
	}
function linkage($link)
	{
		$vlink=array_unique(array_push($_SESSION['sites'],$link));
		$_SESSION['sites'] =$vlink;
		return $link;
	}
?>
<center><h1>This is the main page</h1></center>
<br><br>
<p> visit one of the following pages</p>
<a href="<?php echo linkage("http://localhost/session2.php"); ?>">Page 2 </a><br>
<a href="<?php echo linkage("http://localhost/session3.php"); ?>">Page 3 </a><br>
<a href="<?php echo linkage("http://localhost/session4.php"); ?>">Page 4 </a><br>
<a href="<?php echo linkage("http://localhost/session5.php"); ?>">Page 5 </a><br>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: trouble with arrays and $_SESSION

Posted 06 May 2010 - 09:10 AM

You're not treating $_SESSION['sites'] as an array, and it's letting you know that. You should experiment with printing arrays in PHP...it's a good way to get the feel of the language. An example:

<?php

$a['test'] = "hello";
$a['langs'][] = "hola";
$a['langs'][] = "bon jour";
$a['langs'][] = "ni hao";

print_r($a);

asort($a['langs']);

print_r($a);

?>



yields

$ php -f myarray.php 
Array
(
    [test] => hello
    [langs] => Array
        (
            [0] => hola
            [1] => bon jour
            [2] => ni hao
        )

)
Array
(
    [test] => hello
    [langs] => Array
        (
            [1] => bon jour
            [0] => hola
            [2] => ni hao
        )

)



so $a an array. $a['test']? Not an array. $a['langs']? DEFINITELY an array.
Was This Post Helpful? 0
  • +
  • -

#7 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 07 May 2010 - 01:55 PM

I made some changes in the script. My fear now is the $_session variable wants a multidimesional array because I'm afraid my function call is be included in the href. I don't know. I'm shooting wildly. It seems to me that the href is not reading the url but some of the function and the url. I thought the function would be easier otherwise I will need a plethora of the same statements in each href, which in my opinion is bad practice, and certainly not the way to learn to use functions when they are useful. I try to use what I have learned thus far in my scripts to prep me for later chapters, rather than going straight for coding every line.

<?php
	session_start();
?>
<html>
<body>
<?php
if (empty($_SESSION)) 
	{
		$_SESSION[1]="http://localhost/session1.php";
	}
function linkage($link)
	{
		$alink[]=$link;
		$vlink=array_unique(array_push($_SESSION,$alink));
		$_SESSION =$vlink;
		return $link;
	}
?>
<center><h1>This is the main page</h1></center>
<br><br>
<p> visit one of the following pages</p>
<a href="<?php echo linkage("http://localhost/session2.php"); ?>">Page 2 </a><br>
<a href="<?php echo linkage("http://localhost/session3.php"); ?>">Page 3 </a><br>
<a href="<?php echo linkage("http://localhost/session4.php"); ?>">Page 4 </a><br>
<a href="<?php echo linkage("http://localhost/session5.php"); ?>">Page 5 </a><br>
</body>
</html>



I appreciate the help. Special thanks to the Union Jack aka JackofAllTrades. You've all been helpful so far.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: trouble with arrays and $_SESSION

Posted 07 May 2010 - 02:30 PM

If I understand what you're trying to do, here's some code that may help give you some ideas:

<?php

// Note, we use the & to pass the actual array,
// not a copy, so that the array is actually 
// modified in the function, rather than the copy.
function remove_page(&$page_array, $page_num)
{
    $loc = array_search($page_num, $page_array);
    if ($loc !== FALSE)
        unset($page_array[$loc]);
}

function print_pages($page_array)
{
    foreach ($page_array as $page)
        print("{$page}\n");
}

$unvisited = array();
for($i = 1; $i < 8; ++$i)
{
    $unvisited[] = $i;
}

print_pages($unvisited);

// Remove page 5                                                                
print "\n=== Removing Page 5 ===\n";

remove_page($unvisited, 5);

print_pages($unvisited);

?>


Was This Post Helpful? 0
  • +
  • -

#9 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 08 May 2010 - 07:52 AM

What I'm trying to do is when clicking a link to another page is cumulatively store the url in a session variable. Then on all other pages display those urls I've gone to in an ordered list. My thinking is that by clicking the link it loads or pass the url to the function where the url is then added to the end of the session variable. I use the array_unique to eliminate repeated urls. I then return the url that was passed to the function, so that the link is read correctly when clicked and passes me to the correct page.
Was This Post Helpful? 0
  • +
  • -

#10 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 10 May 2010 - 08:40 AM

Ok I never figured out why I was getting that error, but I did manage to get rid of it by removing the array_push as I didn't need it. Now it goes to the proper page when clicked. The new page appears fine but the links that are supposed to be listed are empty.

<?php
	session_start();
?>
<html>
<body>
<center><h1>This is the Second Page</h1></center>
<br><br><br><br><br><br><br><br><br>
<h3>Pages already visited</h3>
<?php
function linkage($link)
		{
		$_SESSION[]=$link;
		$vlink=array_unique($_SESSION);
		$_SESSION =$vlink;
			}
	?>
<ol>
<?php
	foreach ($_SESSION as $s)
		{
		switch ($s)
			{
			case "http://localhost/session1.php":
				$name="Main Page";
				break;
			case "http://localhost/session2.php":
					$name="Page2";
					break;
			case "http://localhost/session3.php":
				$name="Page 3";
				break;
			case "http://localhost/session4.php":
				$name="Page 4";
				break;
			case "http://localhost/session5.php":
				$name="Page 5";
				break;
			}
		if ($name=="Page 2") {continue;}	
		echo "<li><a href=\"".linkage($s)."\">".$name."</a>";
		}
?>
</ol>
</body>
</html>




Here I am trying to list the pages I've already visited as links minus the page I'm currently on.
Was This Post Helpful? 0
  • +
  • -

#11 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: trouble with arrays and $_SESSION

Posted 10 May 2010 - 02:55 PM

The function linkage doesn't return anything, that's why the links are empty. You can run the check inside the linkage function and have it return the path.
Was This Post Helpful? 0
  • +
  • -

#12 jptenney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-April 10

Re: trouble with arrays and $_SESSION

Posted 11 May 2010 - 07:57 AM

Ok I've got it work except for one final thing. It displays all links in the visited links list even if I never clicked on some of those links. So now I'm wondering if all my function calls are run despite being in a link. In other words I apparently don't need to click the link in the first page for the function to get called. If that is the case, how would I work around that?

Page1
<?php
	session_start();
?>
<html>
<body>
<?php
	if (empty($_SESSION['sites'])) 
		{
			$_SESSION['sites'][]="http://localhost/session1.php";
		}
	function linkage($link)
		{
		$_SESSION['sites'][]=$link;
		$vlink=array_unique($_SESSION['sites']);
		$_SESSION['sites'] =$vlink;
		return $link;
		}
?>
<center><h1>This is the main page</h1></center>
<br><br>
<p> visit one of the following pages</p>
<a href="<?php echo linkage("http://localhost/session2.php"); ?>">Page 2 </a><br>
<a href="<?php echo linkage("http://localhost/session3.php"); ?>">Page 3 </a><br>
<a href="<?php echo linkage("http://localhost/session4.php"); ?>">Page 4 </a><br>
<a href="<?php echo linkage("http://localhost/session5.php"); ?>">Page 5 </a><br>
</body>
</html>




Page 2
<?php
	session_start();
?>
<html>
<body>
<center><h1>This is the Second Page</h1></center>
<br><br><br><br><br><br><br><br><br>
<h3>Pages already visited</h3>
<ol>
<?php
	foreach ($_SESSION['sites'] as $s)
		{
		switch ($s)
			{
			case "http://localhost/session1.php":
				$name="Main Page";
				break;
			case "http://localhost/session2.php":
					$name="Page2";
					break;
			case "http://localhost/session3.php":
				$name="Page 3";
				break;
			case "http://localhost/session4.php":
				$name="Page 4";
				break;
			case "http://localhost/session5.php":
				$name="Page 5";
				break;
			}
		if ($name=="Page 2") {continue;}	
		echo "<li><a href=\"".$s."\">".$name."</a><br>";
		}
?>
</ol>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1