homepage Welcome to WebmasterWorld Guest from 23.20.77.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

This 46 message thread spans 2 pages: 46 ( [1] 2 > >     
HIding Javascript
can't figure it out
usavetele




msg:1252760
 4:01 pm on Jul 10, 2005 (gmt 0)

Ok, I've found the following for hiding Javascript, but it doesn't work for me. From the coding below, where does the second piece of coding go?

Here's what I found in an old Javascript forum, can't figure it out:

Don't want your javascript copied? Here's a very simple script that will hide it!

On the page where the javascript is placed, add:

<?
session_start();
if(!session_is_registered('allow_script'))
{
session_register('allow_script');
$allow_script = true;
}
?>
<html>
<head>
<script language="javascript" src="script.php"></script>
</head>
<body>
Body goes here...
</body>
</html>

And now create a new file called script.php and place your javascript there:

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

alert("Woohoo! My javascript Works!");

<?
$allow_script = false;
}
?>

 

jatar_k




msg:1252761
 4:08 pm on Jul 10, 2005 (gmt 0)

what exactly is happening?
or does it just not do anything?

usavetele




msg:1252762
 4:23 pm on Jul 10, 2005 (gmt 0)

Hi!

It does nothing because I don't know where to put this coding:

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

alert("Woohoo! My javascript Works!");

<?
$allow_script = false;
}
?>

Or is this for only testing all the coding?

jatar_k




msg:1252763
 4:27 pm on Jul 10, 2005 (gmt 0)

that is for testing it

you would creat a file called script.php in the same directory as the first page and that would be the content of that file

usavetele




msg:1252764
 4:37 pm on Jul 10, 2005 (gmt 0)

Yip, I believe I did that correctly. This is what I dumped into the script.php file, which is located in the same directory as my web page that has the following coding:

<script type="text/javascript">
var exchRate = 1.20379;
function getUSD(price) {
var USD = price * exchRate;
if (USD.toFixed) { // check whether the browser supports that method
USD = USD.toFixed(2); // (2 is the required number of decimals)
} else {
USD = Math.round(USD*100)/100; // less perfect for older browsers
}
USD = '<b>USD $' + USD + '<\/b>'; // concatenation. escape the slash after the <
document.write(USD); // write the result
}
</script>

I have also tried putting a "/" in this tag:
<script language="javascript" src="/script.php"> getUSD('42.99'); </script>

But this doesn't make the code work. But when I add the following coding it works:

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

alert("Woohoo! My javascript Works!");

<?
$allow_script = false;
}
?>

I'm baffled what I'm doing wrong. Thanks for your help!

jatar_k




msg:1252765
 4:47 pm on Jul 10, 2005 (gmt 0)

I think script.php should look something like this

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

var exchRate = 1.20379;
function getUSD(price) {
var USD = price * exchRate;
if (USD.toFixed) { // check whether the browser supports that method
USD = USD.toFixed(2); // (2 is the required number of decimals)
} else {
USD = Math.round(USD*100)/100; // less perfect for older browsers
}
USD = '<b>USD $' + USD + '<\/b>'; // concatenation. escape the slash after the <
document.write(USD); // write the result
}

<?
$allow_script = false;
}
?>

usavetele




msg:1252766
 10:55 pm on Jul 10, 2005 (gmt 0)

Ack, I can't get this to work:

This is in my header:

<?
session_start();
if(!session_is_registered('allow_script'))
{
session_register('allow_script');
$allow_script = true;
}
?>

This is in my "script.php":

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

var exchRate = 1.20379;
function getUSD(price) {
var USD = price * exchRate;
if (USD.toFixed) { // check whether the browser supports that method
USD = USD.toFixed(2); // (2 is the required number of decimals)
} else {
USD = Math.round(USD*100)/100; // less perfect for older browsers
}
USD = '<b>USD $' + USD + '<\/b>'; // concatenation. escape the slash after the <
document.write(USD); // write the result
}

<?
$allow_script = false;
}
?>

This is what's in the body of my page, where the end result should be (note the "/", is this right or wrong?):

<script language="javascript" src="/script.php"> getUSD('42.99'); </script>

This doesn't work, but the following coding works if I add it below the coding directly above:

<?
session_start();
if($allow_script)
{
header("Content-type: text/javascript");
?>

alert("Woohoo! My javascript Works!");

<?
$allow_script = false;
}
?>

Needless to say, I'm at a loss as to what the problem is. I know next to nothing about Javascript of course.

Any help would be much appreciated!

THANKS!

jatar_k




msg:1252767
 6:47 pm on Jul 11, 2005 (gmt 0)

I know that I am taking a totally different direction here and I apologize if it really messes you up.

why worry about hiding js, why not do it with php and then never worry about people seeing the code (since it is server side)

your function in php

function getUSD($price) { 
$exchRate = 1.20379;
$USD = '<b>USD $' . number_format($price * $exchRate,2) . '</b>';
return $USD;
}

and then you call it this way

<?php echo getUSD(42.99);?>

isn't that much easier?

usavetele




msg:1252768
 3:17 am on Jul 12, 2005 (gmt 0)

Wow! That's a really interesting idea!

Not only do I know next to nothing about Javascript (no laughing!), but I know NOTHING about PHP. I'm learning though! Anyway, here goes the stupid questions:

Where does the following coding go ("script.php" file?)
function getUSD($price) { $exchRate = 1.20379; $USD = '<b>USD $' . number_format($price * $exchRate,2) . '</b>'; return $USD; }

Also, is it possible to duplicate this function on the same page numerous times? I'm wanting to also convert prices from EURO to GBP, YEN and CAD. Is it as simple as changing the USD in the coding you suggested to GBP, etc. and then naming those files different than script.php?

I couldn't get the coding you gave me to work because I didn't know what to insert in the header and where to insert this coding:

function getUSD($price) { $exchRate = 1.20379; $USD = '<b>USD $' . number_format($price * $exchRate,2) . '</b>'; return $USD; }

This is exciting if I can use coding that is much shorter. Very interesting!Thanks so much for your help.

usavetele




msg:1252769
 3:37 am on Jul 13, 2005 (gmt 0)

Hi Guys!

I'd appreciate any help on this.

Thanks!

jatar_k




msg:1252770
 5:48 pm on Jul 13, 2005 (gmt 0)

sorry usavetele,

spent yesterday with my daughter so I don't really think of anything else at those times, strangely no one ever seems to walk on threads that I am dealing with ;)

so, a few questions to better understand

>> EURO to GBP, YEN and CAD.

do you have a base currency that is getting converted to all of these other currencies or is it an any to any situation?

do you have a list of exchange rates somewhere to do all of these conversions? or are you jst going to put them all in the code as with the us rate

the function I posted can be changed to accomodate for whatever you need to do. Though I would need to know these answers first. Try explaing the actual business logic that you want to have and I will see if I can't explain how to transform that into proper code

usavetele




msg:1252771
 1:42 am on Jul 14, 2005 (gmt 0)

I can totally relate to you wanting to spend undivided time with your daughter. I was a stay-at-home mom for 5 years until going back to work a year ago. I hope you had a great time with her. I enjoy my time with our daughter too. :)

Thank you so much for taking the time to understand my currency converter problem. I'll answer your Qs now.

"do you have a base currency that is getting converted to all of these other currencies or is it an any to any situation?"

Yes, I want to replicate the affiliate program I joined which is based in France. Their prices are shown in Euros, but are also converted from Euros to USD, GBP, CAD and YEN. I have asked for their assistance and they suggested setting a variable on my site and updating the exchange rates once a week. Obviously they haven't given me the coding for that even though I asked. UGH!

"do you have a list of exchange rates somewhere to do all of these conversions? or are you jst going to put them all in the code as with the us rate"

I signed up for Oanda.com's email service which sends me the exchange rates daily. I set it up to get the exchange rates from EUROS to each of the following: USD, GBP, CAD and YEN. The goal of course is to update the exchange rates each day or week, and having them change throughout my entire site.

I get confused very easily with javascript and php since these are very new to me. I'd really like to know how to create the coding for each of the conversion from Euros and to know exactly where to put the coding on my pages/directories.

Thanks again for taking the time to listen to what I'm looking do to on my site. :)

jd01




msg:1252772
 3:12 am on Jul 14, 2005 (gmt 0)

I don't usually post in here, but I am a little bored today, so I will chime in for a minute:

If you create a file called exchange.php (or anything you want), you can then put the conversion code in a single file that will be used on all pages that need conversions - let me explain...

1st since you have a number of conversions, and will be updating them weekly, we should make the exchange rates easy to get at, so at the top of the exchange.php file you could put this:

<?php
$USexchRate = 1.20379;
$YENexchRate = .78464;

and so on for each currency... then after defining the variables (in the same file) you can create the code that will actually convert the exchange rate:

function getUSD($price) {
global $USexchRate; // get the exchange rate previously defined. function.
$USD = '<b>USD $' . number_format($price * $USexchRate,2) . '</b>';
return $USD;
}

function getYEN($price) {
global $YENexchRate; // get the exchange rate previously defined.
$YEN = '<b>YEN $' . number_format($price * $YENexchRate,2) . '</b>';
return $YEN;
}

and so on for each currnecy/exchange rate you have defined.

So this far we have a file called 'exchange.php' which looks like this (only longer in real life)

<?php
$USexchRate = 1.20379;
$YENexchRate = .78464;

function getUSD($price) {
global $USexchRate; // get the exchange rate we have defined.
$USD = '<b>USD $' . number_format($price * $USexchRate,2) . '</b>';
return $USD;
}

function getYEN($price) {
global $YENexchRate; // get the exchange rate we have defined.
$YEN = '<b>YEN $' . number_format($price * $YENexchRate,2) . '</b>';
return $YEN;
}
?>

Now we have to get the file into the pages we would like to use the exchange rate on. At the top of each page we will tell your server to include it. (This will be the first line of the page, even above the 'doc type' or any html is fine.)

<?php include "exchange.php";?>

This code will cause the file you have just created, 'exchange.php', to be 'part of' the page that is opening, so you can add the above line of code to each page you need an exchange rate on and the 'exchange.php' will be 'included' on each of them.

Finally, you will need to get the numbers back out of the exchange rate functions so we can show the right amounts:

If the price is $42.99 and you would like to convert (getUSD(), does the converting) and display (echo 'value' does the displaying) US dollars, you would use:

<?php echo getUSD(42.99);?>

To convert to YEN, you would use:

<?php echo getYEN(42.99);?>

So the final .html page (probably actually php, but either way it is the main page that will display all the information.) will look something like this:

<?php include "exchange.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<table> The real price of this item is: $42.99<br>
The USD conversion is: <?php echo getUSD(42.99);?><br>
The YEN conversion is: <?php echo getYEN(42.99);?>
</table>

Hope this helps. Have fun with php.

Justin

jatar_k




msg:1252773
 3:24 am on Jul 14, 2005 (gmt 0)

>> I don't usually post in here

geez, if they are all going to be that good then you should more often ;)

usavetele, try that and see if it makes sense, if not then we can explain a little more

usavetele




msg:1252774
 3:12 am on Jul 15, 2005 (gmt 0)

WOOOOOOOOW! That looks like it'll work out very nicely. I just love the way you explained how it works. Very simple laymen's terms. :) I'm off to try it now.

Words can't describe how much a appreciate the help both of you guys have given me.

THANK YOUUUUUUUUUUUUUUUUUU!

I'll let you know how I do with the coding.

jatar_k




msg:1252775
 3:14 am on Jul 15, 2005 (gmt 0)

wow, that's a lot of vowels ;)

come back and post if you have any problems. There are some ways to make that all shorter but it doesn't reaaly seem necessary if it works.

jd01




msg:1252776
 3:22 am on Jul 15, 2005 (gmt 0)

Glad I could help...

I'll try to visit more often, but I write php mini-apps about 4 days a week, so when I take a break to post, I normally stick with something different, like opinions or mod_rewrite.

Justin

usavetele




msg:1252777
 3:52 am on Jul 15, 2005 (gmt 0)

jd01,

I tried your coding and directions word for word and can't get the conversion to show up. Hmmm...wonder what I've done wrong.

usavetele




msg:1252778
 3:55 am on Jul 15, 2005 (gmt 0)

Perhaps I need to save the exchange.php in a directory different from the web page I put the coding?

jatar_k




msg:1252779
 4:51 am on Jul 15, 2005 (gmt 0)

no, if the include is as above

<?php include "exchange.php";?>

then that is pointed at the same directory

usavetele




msg:1252780
 3:53 am on Jul 16, 2005 (gmt 0)

Wonder why it's not working then. Hmmm!

I'll be out of town with no access to email until Wednesday or Thursday. I'll check back then.

jatar_k




msg:1252781
 6:32 pm on Jul 16, 2005 (gmt 0)

well since I can see the php code in the view source from the browser and the page extension is html then I have some questions you will need to ask your host, if you don't already know the answers.

Do you have php support with your hosting plan?
if you do ask them if it is only for the .php extension?
if so, then ask them if they allow .htacces files?
if so, then we can explain how to make an htaccess file and get the html extension parsed for php code.

usavetele




msg:1252782
 11:14 pm on Jul 20, 2005 (gmt 0)

Hello,

I see php stuff in my vdeck and .htacces. Does this help?

Thanks!

usavetele




msg:1252783
 4:17 pm on Jul 21, 2005 (gmt 0)

Help me Rhonda, help, help me Rhonda!

HAHAHA!

Just kidding...I know you're busy.

jatar_k




msg:1252784
 7:25 pm on Jul 21, 2005 (gmt 0)

do you have a way of editing your .htaccess file?

you will need to add php parsing for html files

which would mean you need to add something like in this thread
[webmasterworld.com...]

there are some good links to resources in that thread as well

usavetele




msg:1252785
 7:56 pm on Jul 21, 2005 (gmt 0)

I use CoffeeCup 6.0 to edit my web sites. Can I assume this is what you are asking? I looked at that thread on .htaccess and php parsing, but that's way over my head.

Which of my files are you referring to for having .html? I'm confused because all my pages are and will be .shtml. The only .html I have on my pages are the menu boxes.

mcibor




msg:1252786
 9:00 pm on Jul 21, 2005 (gmt 0)

usavetele, do you really need that code hidden?
If not, then I would recommend using javascript, as it doesn't need refreshing.

With php this is one file method:
exchange.php:
<?php
function exchange($price, $xrate) {
return number_format($price * $xrate, 2);
}

$rate = array(
"USD" => 1.20983,
"YEN" => 0.87635,
"PLN" => 0.45634);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<head></head><body>
<form name="exchange" action="#" method="POST">Type in the price: <input type="text" name="price"><br><input type="submit" name="action" value="Submit"></form>
<?php

if($_POST["action"] == "Submit"){
echo 'The real price of this item is: $'.$_POST["price"].'<br>
The USD conversion is: <b>$'.exchange($_POST["price"], $rate["USD"]).'</b><br>
The YEN conversion is: <b>'.exchange($_POST["price"], $rate["YEN"]).'</b><br>
The PLN conversion is: <b>'.exchange($_POST["price"], $rate["PLN"]).'zł</b><br>';

?>
</body>
</html>

This structure: $rate["USD"] takes value from an array that has key "USD". Array is defined as: $key => $value. Therefore $rate["USD"] is 1.20983.
$_POST on the other hand is also an array. But it contains values of a form. So in this example $_POST will contain such keys: price and action (there are two inputs in the form). If submit button is pressed then the information is sent to that array (_POST) and can be processed.
Hope this helps you.
If you have any questions just ask!
Michal Cibor

usavetele




msg:1252787
 10:34 pm on Jul 21, 2005 (gmt 0)

Very interesting mcibor! I'll give it a try!

I wanted to hide the coding because so many of my competitors selling the same products form the same affiliate don't bother to update the prices via the exchange rates. They just show really outdated prices because they don't have a conversion program running on their pages. I'm afraid they might see my prices, look at my coding and then copy it. I just want as much of a competitive edge as I can get. You're thoughts? Am I worrying for no reason?

Thanks for the idea. I'll try it later tonight when I have more time to mess with it. I really appreciate everyone's help.

Webmaster World is such a great resource!

usavetele




msg:1252788
 11:11 pm on Jul 21, 2005 (gmt 0)

OK, I couldn't wait until tonight to try this new code. I was leary because of the "submit" button in the coding, but thought I'd try it anyway. As I thought, the "customer" would have to do the work to convert the price from one currency to another. The coding is nice, but this is exactly what I'm trying to avoid. I want the potential customs to instantly see the different currencies w/o doing any conversion on their part.

I'll keep trying the coding from Jatar K, but still need help in getting the coding to work with my .htaccess and .php thing-a-ma-jigs. :)

jatar_k




msg:1252789
 5:05 am on Jul 22, 2005 (gmt 0)

well we do have a bit of a problem

that being shtml, now the thing is that usually means SSI is enabled and therefore the files are already being parsed for something and parsing files for 2 things doesn't really work at all, regardless of what links people might find that say otherwise.

So, we are at a bit of a stand still. Changing page names once they are probably already indexed in search engines is never a smart thing to do so we can't switch it to php.

hmm, I'm kinda stumped for the moment. I will think about it more tomorrow.

This 46 message thread spans 2 pages: 46 ( [1] 2 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved