I want to pass two variables in a php url at present I have

$line = "<a href=\"$click?$namehref\"><font color='red'>$name</font> </a>"; 

When I click the link it displays the $click variable, but does not then pass to $namehref which is an mp3 link, what would the syntax be to achieve this.

Recommended Answers

All 16 Replies

Can you please clarify exactly what the values of the variables $name, $namehref, and $click are?

    // Gets File Names
        $name=$dirArray[$index];
        $namehref=$dirArray[$index];

    // Separates directories, and performs operations on those directories
        if(is_dir($dirArray[$index]))
        {
                $extn="&lt;Directory&gt;";
                $size="&lt;Directory&gt;";
                $sizekey="0";
                $class="dir";

            // Gets favicon.ico, and displays it, only if it exists.
                if(file_exists("$namehref/favicon.ico"))
                    {
                        $favicon=" style='background-image:url($namehref/favicon.ico);'";
                        $extn="&lt;Website&gt;";
                    }

            // Cleans up . and .. directories
                if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon.ico);'";}
                if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
        }
                $click = "https://nerjabible.com/output/linkcounter.php";

$name, $namehref, and $click are

The $name is the path and file name of each message file in the directory
The $namehref is the file name of each message file in the directory
The $click is the path and file name of the linkcounter.php which counts the url clicks and writes the click details to a text file.

OK, so then looking at the URL "$click?$namehref" it looks like that translates to a URL such as https://nerjabible.com/output/linkcounter.php?<some directory name>

Query strings need to be a key-value pair, meaning something like keyword=<some value here>. Therefore, the URL should be properly written as https://nerjabible.com/output/linkcounter.php?namehref=<some directory name>. In your PHP code, you would want it to look like this:

$line = "<a href=\"$click?namehref=$namehref\"><font color='red'>$name</font> </a>"; 

Then, in the linkcounter.php file, you can do something such as:

$namehref = $_GET['namehref'];

and that will fetch the parameter from the URL string.

Alternatively, you can do something such as:

$line = "<a href=\"$click?any_variable_name=$namehref\"><font color='red'>$name</font> </a>"; 

and then, in linkcounter.php, you would have:

$variable_retrieved_from_url = $_GET['any_variable_name'];

I hope that this makes sense!! Feel free to ask if you have any follow-up questions to get it working.

Thanks for your help, the problem I keep getting is that the html5 audio player does not display after the count occurs, so with

echo $line = "<a href=\"$click?any_variable_name=$namehref\"><font color='red'>$name</font> </a>";

The count works fine in the first part, but then I get a blank iframe and the audio player for $namehref does not display.

This is what the url displays if I hover over the url link.

https://nerjabible.com/output/linkcounter.php?any_variable_name=48%20The%20Way%20to%20the%20Father%20%20Part%201%20(El%20Camino%20al%20Padre)%205-2-2023.mp3

It seems that the path to $namehref for the audio file is not found when the linkcounter.php is added

so the original line works

echo $line = "<a href=\"$namehref\"><font color='darkviolet'>$name</font></a>";

but not when the linkcounter.php is added, although the count works fine but there is no audio player display

add_email1.png

Hello Dani, I tried every conceivable way, the count works fine but then it does not display the html player, I know it must be syntax, any suggestions?

When you have the URL that includes any_variable_name=$namehref are you also using $GET['any_variable_name'] to retrieve the filename on linkcounter.php?

I tried both but right now I am using

echo $line = "<a href=\"$click?namehref=$namehref\"><font color='red'>$name</font></a>";

and the linkcounter.php

<?php
$count_in = explode(" ", file_get_contents('https://nerjabible.com/output/1st_count.txt'));
$Date = Date("d/m/Y");
$namehref = $_GET['namehref'];
$count = $count_in[0]+1;
$click = $count. " <-count ". " Date = ". $Date. " ". $namehref;
file_put_contents('/home4/ricstrav/domains/nerjabible.com/public_html/output/1st_count.txt', $click);
?>

And, with that code, what gets added to 1st_count.txt? Does linkcounter.php successfully grab the value of namehref that was passed into the URL?

Yes it works well

add_email1.png

Now looking at the screenshot you included in your previous post, the original line says to 'echo' and you replaced it with a line that says to just set it to the variable $line but don't do anything with it (don't echo it). So you would expect that it wouldn't show anything.

Also, I guess I'm confused, what's wrong with the original line that you have?

The echo is to print the file line in the iframe as shown in item no1, with the line below

echo $line = "<a href=\"$click?namehref=$namehref\"><font color='red'>$name</font></a>";

It changes the count and the $namehref line but does not then display the audio player as shown in no2
The original line as shown in no3

 echo $line = "<a href=\"$namehref\"><font color='red'>$name</font> </a>"; 

Shows the audio player as shown in no4

add_email2.png

Here is the code that generates the url links from the directory

<!doctype html>
<html>
<head>
<title>php website template-</title>
<meta http-equiv="Content-Type"  content="text/html charset=UTF-8" />

</head>

<body>
<div id="container">


    <table class="sortable">
        <thead>
        <tr>

        </tr>
        </thead>
        <tbody><?php
$Date = Date("d/m/Y");
    // Adds pretty filesizes
    function pretty_filesize($file) {
        $size=filesize($file);
        if($size<1024){$size=$size." Bytes";}
        elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";}
        elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";}
        else{$size=round($size/1073741824, 1)." GB";}
        return $size;
        $directory=basename(dirname(__DIR__));
        $dir=basename(dirname(__FILE__));
    }

    // Checks to see if veiwing hidden files is enabled
    if($_SERVER['QUERY_STRING']=="hidden")
    {$hide="";
     $ahref="./";
     $atext="Hide";}
    else
    {$hide=".";
     $ahref="./?hidden";
     $atext="Show";}

     // Opens directory
     $myDirectory=opendir(".");

    // Gets each entry
    while($entryName=readdir($myDirectory)) {
       $dirArray[]=$entryName;
    }

    // Closes directory
    closedir($myDirectory);

    // Counts elements in array
    $indexCount=count($dirArray);

    // Sorts files
    sort($dirArray);

    // Loops through the array of files
    for($index=0; $index < $indexCount; $index++) {

    // Decides if hidden files should be displayed, based on query above.
        if(substr("$dirArray[$index]", 0, 1)!=$hide) {

    // Resets Variables
        $favicon="";
        $class="file";

    // Gets File Names
        $name=$dirArray[$index];
        $namehref=$dirArray[$index];

    // Separates directories, and performs operations on those directories
        if(is_dir($dirArray[$index]))
        {
                $extn="&lt;Directory&gt;";
                $size="&lt;Directory&gt;";
                $sizekey="0";
                $class="dir";

            // Gets favicon.ico, and displays it, only if it exists.
                if(file_exists("$namehref/favicon.ico"))
                    {
                        $favicon=" style='background-image:url($namehref/favicon.ico);'";
                        $extn="&lt;Website&gt;";
                    }

            // Cleans up . and .. directories
                if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon.ico);'";}
                if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
        }

    // File-only operations
        else{
            // Gets file extension
            $extn=pathinfo($dirArray[$index], PATHINFO_EXTENSION);

            // Prettifies file type

            switch ($extn){

                case "mp3": $extn="mp3"; break;

                default: if($extn!=""){$extn=strtoupper($extn)." File";} else{$extn="Unknown";} break;
            }

            // Gets and cleans up file size
                $size=pretty_filesize($dirArray[$index]);
                $click = "https://nerjabible.com/output/linkcounter.php";
                if($index ==8) 
                {
//  echo $line = "<a href=\"$namehref\"><font color='red'>$name</font> </a>"; 
        echo $line = "<a href=\"$click?namehref=$namehref\"><font color='red'>$name</font></a>";
       echo "<br />";
                }
                if($index == 9) 
                {
        echo $line = "<a href=\"$click?namehref=$namehref\"><font color='darkviolet'>$name</font></a>";
        echo "<br />";
                }
                if($index == 10) 
                {
        echo $line = "<a href=\"$click?namehref=$namehref\"><font color='green'>$name</font></a>";
        echo "<br />";
                }
            if($index > 10) 
                {
        echo $line = "<a href=\"$namehref\"><font color='blue'>$name</font></a>";
        echo "<br />";
        }}
       }
    }

    ?>
        </tbody>
    </table>
</div>
</body>
</html>

I have this working now using

echo "<a href=\".linkcounter.php\"><font color='red'>$name</font></a>";

with the .linkcounter.php in the same directory as the mp3

.linkcounter.php
    <?php
    $count_in = explode(" ", file_get_contents('https://nerjabible.com/output/1st_count.txt'));
    $Date = Date("d/m/Y");
    $namehref = $_GET['namehref'];
    $namehref = str_replace(' ', '%20', $namehref);
    $count = $count_in[0]+1;
    $click = $count. " <-count ". " Date = ". $Date. " ". $namehref;
    file_put_contents('/home4/ricstrav/domains/nerjabible.com/public_html/output/1st_count.txt', $click);
    ?>
    <audio controls id="myaudio">
       <source src="47%20The%20Way%20to%20the%20Father%20%20Part%202%20(El%20Camino%20al%20Padre)%2012-2-2023.mp3" type="audio/mpeg"> <!ā€“ā€“ this works -->

       But when I try this it does not work
       <source src=<?php"$namehref"?> type="audio/mpeg">

I fixed the issue, thanks

So glad to hear you were able to get it working! Hopefully I was at least a little bit helpful :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.