Jump to content

[SOLVED] function in Loop Fatal error: Cannot redeclare sockaccess() (previously declared


mrprozac

Recommended Posts

Hi,

 

I've wrote a script in php, i'm a beginner with php so it's a mess.i'll clean it up when the followin problem is solved :)

 

I'm trying to put a little script which looks at HTTP HEADERS of external links, if the headers response with the right answer it should print the result. For example:

if header for 'link1' = 200404 then show link

if header for 'link2' = 200200 then skip to next

if header for 'link3' = 404404 then print file not found

 

The reason for "200404" as response is i check and print two headers in test.php, because i link to a search engine which contains lots of old links, most of those domain are moved or replaced by those annoying link site's.

 

 

Here's my index.php which prints the search results.

<?php include("header.php.inc");?>
<?php
if($_GET["search"] == ""){ 
}else{
include('conn.php'); 
$dbname = "seeqpod";
$usertable = "searches";
$song = $_GET['search']; 
if($_GET["page"] == "0"){

$query = "INSERT INTO searches ( term, count ) 
VALUES ('$song','1')";

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable");

mysql_close;
};
};
?>
<?php
$link = mysql_connect("localhost", "user", "pass");
mysql_select_db("db", $link);

$result = mysql_query("Select * from searches where term='$song'", $link);
$num_rows = mysql_num_rows($result);

echo "<center>$song has been searched $num_rows times now</center>";
?>
<?php
  foreach( $track as $value ) 
  { 
    $tasks = $value->getElementsByTagName("title"); 
    $task  = $tasks->item(0)->nodeValue; 
    $artists = $value->getElementsByTagName("creator"); 
    $artist  = $artists->item(0)->nodeValue; 
    $details = $value->getElementsByTagName("location"); 
    $detail  = $details->item(0)->nodeValue; 
$ids  = $value->getElementsByTagName("mp3_url_id");
$id   = $ids->item(0)->nodeValue;


    echo "<small><a href='player.swf?autostart=true&file=$detail&title=$task' class='lightwindow page-options' params='lightwindow_height=15,lightwindow_width=400,lightwindow_show_images=2'>"; echo $lang['play']; echo"</a> ::<a href='$detail"; echo "'>"; echo $lang['download']; echo "</a> ::</small> $task :: <small><a href='?search=$task&x=<?php echo $aantal; ?>' >"; echo $lang['srelevant']; echo "</a></small><br>"; 
  } 


?>
<?php if($_GET["page"]=="0"){ ?>
<a href="?search=<?php 
	echo $zoeken;
?>&lang=<?php
	echo $_GET['lang']
?>&page=<?php echo $next;?>"><img src="images/next.png" alt="<?php echo $lang['next']; ?>"  height="20"/></a>
<? }else{ 
if($_GET["page"]==""){
}else{?>
<a href="?search=<?php 
	echo $zoeken; 
?>&lang=<?php 
	echo $_GET['lang']
?>&page=<?php 
	echo $prev;
?>"><img src="images/prev.png" alt="<?php 
	echo $lang['prev']; 
?>" height="20"/></a> :: <a href="?search=<?php 
	echo $zoeken; 
?>&lang=<?php 
	echo $_GET['lang']
?>&page=<?php 
	echo $next;
?>"><img src="images/next.png" alt="<?php 
	echo $lang['next']; 
?>"  height="20"/></a>
<? } }?>

<?php include("footer.php.inc");?>

 

Here's my test.php with the header check

<?php

$url = $detail;

$host = parse_url($url, PHP_URL_HOST);
$page = parse_url($url, PHP_URL_PATH);

$path_parts = pathinfo(parse_url($url, PHP_URL_PATH));

/*echo $path_parts['dirname'], "\n"; echo "<br />";
echo $path_parts['basename'], "\n"; echo "<br />";
echo $path_parts['filename'], "\n"; echo "<br />"; // since PHP 5.2.0
echo $path_parts['extension'], "\n"; echo "<br />";
*/
$server="$host";
function sockAccess($page)
{
   $errno="";
   $errstr="";
   $fp=0;
   global $server;
   $fp=fsockopen($server,80,$errno,$errstr,30);

   if($fp===0)
   {
      die("Error $errstr ($errno)");
   }
   //$page = "$path";
   $out="GET $page HTTP/1.1\r\n";
   $out.="Host: $server\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4));
   fclose($fp);
   return intval($code);
}

echo /*"CODE=", " ", */sockAccess("$page"), "";

echo /*"CODE=", " ", */sockAccess("/FASLE.phhp"), "";
/*
echo "CODE=", " ", sockAccess("brokenlink.php"), "<br />", "Fetching headers for : ", $server, $page, "<br />", $_GET['x'];
*/

?>

 

I want to put test.php in the loop in index.php, but when i do that i get the following error:

Fatal error: Cannot redeclare sockaccess() (previously declared in /home/mr.prozac/domains/balancehost.co.cc/public_html/dev/music/index.php:53) in /home/mr.prozac/domains/balancehost.co.cc/public_html/dev/music/index.php on line 53

 

<?php include("header.php.inc");?>
<?php
if($_GET["search"] == ""){ 
}else{
include('conn.php'); 
$dbname = "seeqpod";
$usertable = "searches";
$song = $_GET['search']; 
if($_GET["page"] == "0"){

$query = "INSERT INTO searches ( term, count ) 
VALUES ('$song','1')";

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable");

mysql_close;
};
};
?>
<?php
$link = mysql_connect("localhost", "user", "pass");
mysql_select_db("db", $link);

$result = mysql_query("Select * from searches where term='$song'", $link);
$num_rows = mysql_num_rows($result);

echo "<center>$song has been searched $num_rows times now</center>";
?>
<?php
  foreach( $track as $value ) 
  { 
    $tasks = $value->getElementsByTagName("title"); 
    $task  = $tasks->item(0)->nodeValue; 
    $artists = $value->getElementsByTagName("creator"); 
    $artist  = $artists->item(0)->nodeValue; 
    $details = $value->getElementsByTagName("location"); 
    $detail  = $details->item(0)->nodeValue; 
$ids  = $value->getElementsByTagName("mp3_url_id");
$id   = $ids->item(0)->nodeValue;
$url = $detail;

$host = parse_url($url, PHP_URL_HOST);
$page = parse_url($url, PHP_URL_PATH);

$path_parts = pathinfo(parse_url($url, PHP_URL_PATH));

/*echo $path_parts['dirname'], "\n"; echo "<br />";
echo $path_parts['basename'], "\n"; echo "<br />";
echo $path_parts['filename'], "\n"; echo "<br />"; // since PHP 5.2.0
echo $path_parts['extension'], "\n"; echo "<br />";
*/
$server="$host";
function sockAccess($page)
{
   $errno="";
   $errstr="";
   $fp=0;
   global $server;
   $fp=fsockopen($server,80,$errno,$errstr,30);

   if($fp===0)
   {
      die("Error $errstr ($errno)");
   }
   //$page = "$path";
   $out="GET $page HTTP/1.1\r\n";
   $out.="Host: $server\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4));
   fclose($fp);
   return intval($code);
}

echo /*"CODE=", " ", */sockAccess("$page"), "";

echo /*"CODE=", " ", */sockAccess("/FASLE.phhp"), "";
/*
echo "CODE=", " ", sockAccess("brokenlink.php"), "<br />", "Fetching headers for : ", $server, $page, "<br />", $_GET['x'];
*/

    echo "<small><a href='player.swf?autostart=true&file=$detail&title=$task' class='lightwindow page-options' params='lightwindow_height=15,lightwindow_width=400,lightwindow_show_images=2'>"; echo $lang['play']; echo"</a> ::<a href='$detail"; echo "'>"; echo $lang['download']; echo "</a> ::</small> $task :: <small><a href='?search=$task&x=<?php echo $aantal; ?>' >"; echo $lang['srelevant']; echo "</a></small><br>"; 
  } 


?>
<?php if($_GET["page"]=="0"){ ?>
<a href="?search=<?php 
	echo $zoeken;
?>&lang=<?php
	echo $_GET['lang']
?>&page=<?php echo $next;?>"><img src="images/next.png" alt="<?php echo $lang['next']; ?>"  height="20"/></a>
<? }else{ 
if($_GET["page"]==""){
}else{?>
<a href="?search=<?php 
	echo $zoeken; 
?>&lang=<?php 
	echo $_GET['lang']
?>&page=<?php 
	echo $prev;
?>"><img src="images/prev.png" alt="<?php 
	echo $lang['prev']; 
?>" height="20"/></a> :: <a href="?search=<?php 
	echo $zoeken; 
?>&lang=<?php 
	echo $_GET['lang']
?>&page=<?php 
	echo $next;
?>"><img src="images/next.png" alt="<?php 
	echo $lang['next']; 
?>"  height="20"/></a>
<? } }?>

<?php include("footer.php.inc");?>

 

 

Link to comment
Share on other sites

Functions consist of two parts, the function definition and the calls to that function. You might need to call that function 10 or 100 times, but you only define the function once.

 

Move the function definition to be before the loop. It can actually be anywhere (except when conditionally defined) relative to the function calls, but good programming practice is to place a function definition before the first function call.

Link to comment
Share on other sites

Functions consist of two parts, the function definition and the calls to that function. You might need to call that function 10 or 100 times, but you only define the function once.

 

Move the function definition to be before the loop. It can actually be anywhere (except when conditionally defined) relative to the function calls, but good programming practice is to place a function definition before the first function call.

 

thanks for the information, but the function relies on information from the loop.

It requires the link from the loop.

"$detail" contains the link, and is defined in the loop

Link to comment
Share on other sites

This

<?php
function sockAccess($page)
{
   $errno="";
   $errstr="";
   $fp=0;
   global $server;
   $fp=fsockopen($server,80,$errno,$errstr,30);

   if($fp===0)
   {
      die("Error $errstr ($errno)");
   }
   //$page = "$path";
   $out="GET $page HTTP/1.1\r\n";
   $out.="Host: $server\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4));
   fclose($fp);
   return intval($code);
}
?>

is your function. I don't see a reference to the variable "$detail" in the above code. If you need something in the function, just pass it in.

 

Ken

Link to comment
Share on other sites

This

<?php
function sockAccess($page)
{
   $errno="";
   $errstr="";
   $fp=0;
   global $server;
   $fp=fsockopen($server,80,$errno,$errstr,30);

   if($fp===0)
   {
      die("Error $errstr ($errno)");
   }
   //$page = "$path";
   $out="GET $page HTTP/1.1\r\n";
   $out.="Host: $server\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4));
   fclose($fp);
   return intval($code);
}
?>

is your function. I don't see a reference to the variable "$detail" in the above code. If you need something in the function, just pass it in.

 

Ken

$url = $detail;

 

$host = parse_url($url, PHP_URL_HOST);

$page = parse_url($url, PHP_URL_PATH);

 

$path_parts = pathinfo(parse_url($url, PHP_URL_PATH));

 

/*echo $path_parts['dirname'], "\n"; echo "<br />";

echo $path_parts['basename'], "\n"; echo "<br />";

echo $path_parts['filename'], "\n"; echo "<br />"; // since PHP 5.2.0

echo $path_parts['extension'], "\n"; echo "<br />";

*/

$server="$host";

function sockAccess($page)

 

As you can see the $page links through $url to $detail

Link to comment
Share on other sites

I have a bold suggestion that will only take like 2-3 minutes of your time, try moving the function definition like we have told you it needs to be and see if it works.

 

i did it, but what will i need to put in the loop to call the function? Now it's doing nothing

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.