Page 1 of 1

PHP script issues escaping to html

Posted: Sat Apr 17, 2010 11:48 am
by markosjal
I am working on a image/link rotation script and I am somewhat of a noob to php.

I have something that looks like this:

Code: Select all


<! here is a random number generator>

<?php
for ($i = 0, $z = strlen($a = '0123456789')-1, $s = $a{rand(0,$z)}, $i = 

1; $i != 1; $x = rand(0,$z), $s .= $a{$x}, $s = ($s{$i} == $s{$i-1} ? 

substr($s,0,-1) : $s), $i=strlen($s));
?>


<?php
function rand_chars($c, $l, $u = FALSE) {
 if (!$u) for ($s = '', $i = 0, $z = strlen($c)-1; $i < $l; $x = 

rand(0,$z), $s .= $c{$x}, $i++);
 else for ($i = 0, $z = strlen($c)-1, $s = $c{rand(0,$z)}, $i = 1; $i != 

$l; $x = rand(0,$z), $s .= $c{$x}, $s = ($s{$i} == $s{$i-1} ? 

substr($s,0,-1) : $s), $i=strlen($s));
 return $s;
}
?> 

<!this generates the first ad URLs and filenames based on $catid and random number >

<p align="right">
<a href="/buttons/<? echo $catid; ?>/ad<? echo $s; ?>.htm">
<img src="/buttons/<? echo $catid; ?>/ad<? echo $s; ?>.gif" alt="" border="0"></a> 

<!this generates the second ad URLs and filenames based on $catid and random number with a 1 subtracted >

<p align="right">
<a href="/buttons/<? echo $catid; ?>/ad<? echo $s -1; ?>.htm">
<img src="/buttons/<? echo $catid; ?>/ad<? echo $s -1; ?>.gif" alt="" border="0"></a>
<br>

I want to be able to do a couple of things that I am not having success with

1) Need to be able to call to an adX.txt file that would have its location constructed in php in the same way that the image and link url are constructed above . I think I need only echo this file contents as it would simply be raw html code

2) I need to be able to tell the script that if the value of "$s" is "0" that "0" -1 is 9. What the script returns now for the urls shows a -1 in the filename.

3) I would like to be able to fill the 'alt' tag as well using yet another file, or perhaps the same text file in 1


I think I am messing up somewhere on syntaxes especially where I am escaping to HTML and back to PHP , but I can not be sure. IO have tried several solutions and can not seem to get any to work

Any help appreciated

thanks

Re: PHP scrip issues escaping to html

Posted: Sun Apr 18, 2010 12:20 am
by markosjal
this is more information on one of the objectives

I want to use something like

Code: Select all

echo file_get_contents( URL CONSTRUCTED OF VARIABLES );
or

Code: Select all

readfile( URL CONSTRUCTED OF VARIABLES );
the construct of the URL is as follows

Code: Select all

"/buttons/<?  echo $catid;  ?>/ad<? echo $s -1; ?>.txt"

I just can not get this URL variable result to work inside of the brackets, and I am 90% sure there is simply some advaced syntax that I am missing for escaping in and out of HTML

Re: PHP scrip issues escaping to html

Posted: Sun Apr 18, 2010 6:11 am
by lunarnet76
hi,

Code: Select all

function rand_chars($c, $l, $u = FALSE)
could you please name your variables, I don't want to spend 10min figuring out what is your code doing^^ thanks!

Re: PHP scrip issues escaping to html

Posted: Sun Apr 18, 2010 6:30 am
by lunarnet76

Code: Select all

<?php
for ($i = 0, $z = strlen($a = '0123456789')-1, $s = $a{rand(0,$z)}, $i = 

1; $i != 1; $x = rand(0,$z), $s .= $a{$x}, $s = ($s{$i} == $s{$i-1} ? 

substr($s,0,-1) : $s), $i=strlen($s));
?>
... what the hell is this code!? Are you actually trying to have simply random number between 0 and 9!? you can do simply $number=rand(0,9);

$i=0 is replaced by your $i=1 so it's useless, your for loop stops before the first time as $i is always equals to 1 so it means that only

Code: Select all

$i = 0, $z = strlen($a = '0123456789')-1, $s = $a{rand(0,$z)}, $i = 1;
is executed ...

Re: PHP scrip issues escaping to html

Posted: Sun Apr 18, 2010 8:39 am
by markosjal
the random number generator is not the issue. It IS working and it gives me some flexibilities that I need later such as adding letters, and as many characters as I specify. It probably need not have been posted .

If I were an expert , I would not be posting on this forum, asking about how to set the proper syntax of the mix of HTML and PHP

The VAriables used to construct the URL are in the first post, but here it is again

Code: Select all

/buttons/<?  echo $catid;  ?>/ad<? echo $s; ?>.tvt
%catid refers in this case to a directory and returns a number
$s is the random number generated earlier

my goal is only to echo the info in the .txt file

My problem appears to be the syntax in constructing the URL and then successfully echoing it, as I mentioned earlier.

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 9:00 am
by lunarnet76
sorry I did not understood your issue at first,

you need to use <?php instead of <? and it's working

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 9:03 am
by lunarnet76
1) Need to be able to call to an adX.txt file that would have its location constructed in php in the same way that the image and link url are constructed above . I think I need only echo this file contents as it would simply be raw html code
=> use <?php ALWAYS, never use <?

2) I need to be able to tell the script that if the value of "$s" is "0" that "0" -1 is 9. What the script returns now for the urls shows a -1 in the filename.

Code: Select all

<a href="/buttons/<?php echo $catid; ?>/ad<?php echo $s==0?9:$s; ?>.htm">

3) I would like to be able to fill the 'alt' tag as well using yet another file, or perhaps the same text file in 1

Code: Select all

<img src="/buttons/<? echo $catid; ?>/ad<? echo $s; ?>.gif" alt="<?php echo $catid;?>" border="0"></a> 

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 10:00 am
by markosjal
Perhaps I am an idiot, but let me again clarify

I can do this

Code: Select all

<?
echo file_get_contents("www.mydomain.com/test.txt");
?>
and I can do this

Code: Select all

<?
p align="right">
<a href="/buttons/<? echo $catid; ?>/ad<? echo $s -1; ?>.htm">
<img src="/buttons/<? echo $catid; ?>/ad<? echo $s -1; ?>.gif" alt="" border="0"></a>
?>
what I can not seem to do would be something like this in addition to what was in the original post
<?
echo file_get_contents("/buttons/<? echo $catid; ?>/ad<? echo $s -1; ?>.txt");
?>

I think I do not know how to do the syntax as the $s variable shows empty on the end result

thanks

Mark

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 10:16 am
by markosjal
I just tried the $catid variable in the alt text,

Code: Select all

<p align="right">
<a href="/buttons/<? echo $xcatid; ?>/ad<? echo $s; ?>.htm">
<img src="/buttons/<? echo $xcatid; ?>/ad<? echo $s; ?>.gif" alt="<?php echo $catid; ?>" border="0">

also 

<p align="right">
<a href="/buttons/<? echo $xcatid; ?>/ad<? echo $s; ?>.htm">
<img src="/buttons/<? echo $xcatid; ?>/ad<? echo $s; ?>.gif" alt="<? echo $catid; ?>" border="0">
and here is what my browser got, the alt field is empty

Code: Select all

<p align="right">
<a href="/buttons/11/ad0.htm">
<img src="/buttons/11/ad0.gif" alt="" border="0"></a>
this is what I am saying is that I can not plug variables in and have them work!

Clearly the $catid variable is working as it returned "11" as a subdirectory in the URL

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 11:13 am
by lunarnet76
you have not defined $catid anywhere, that is why the field appear empty!

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 12:51 pm
by markosjal
Actually you are right, so many versions of this and variable names

I corrected that and the alt text will now work with that variavble
I am working on this now.

But here is the other battle
<?
echo file_get_contents("/buttons/<? echo $xcatid; ?>/ad<? echo $s;
?>.txt");

I think I must have a syntax incorrect on this . I think the problem may be all of the nesting?

Re: PHP script issues escaping to html

Posted: Sun Apr 18, 2010 6:17 pm
by lunarnet76
replace all your <? by <?php ALWAYS

Re: PHP script issues escaping to html

Posted: Mon Apr 19, 2010 12:53 am
by Architek
This is how I would write the line.

Code: Select all

<?php

echo file_get_contents("/buttons/".$xcatid."/ad".$s.".txt");

?> 

Re: PHP script issues escaping to html

Posted: Mon Apr 19, 2010 1:21 pm
by markosjal
thanks guys you were a BIG help for this noob!
I ended up doing this however which allows me to use a single text file per directory, and reading only the relevant line


$linenum = $s; //will read line of the file based on variable.
$path = "http://www.domain.com/buttons/$xcatid/titles.htm";
$handle = fopen($path, "r");
$lines=file($path); //$lines is an array
echo ($lines[$linenum]);

I am posting another topic that I will need help with if anyone can help!

Thanks again