LS97 wrote:
sparks wrote:
LS97 wrote:
It doesn't work because you forgot to add javascript double quotes outside the PHP echo.sparks wrote:
Hmmm, well I haven't tested it but I can't see anything wrong with it. You could try putting the php echo in the if statement into speech marks, otherwise your contents might be what's causing the problem, not the statement above.
WindowsExplorer wrote:
Still not working.
oh, oops.
Anyway, why don't you just use PHP since you can?
header('Location')?
Whats' the code anyways? I don't mind what it's written in, I just need it.
Offline
I usually don't redirect in js, I just thought that's what you wanted. Php redirect is very good, nice and fast. Simply put the code
<?php header("your url"); ?> in your page. There are conditions, the redirect must come before any other php on that page, and variables cannot be included as part of the redirect URL. if you want to redirect with HTML, which is slower but does not have these limitations, use the code
<meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com">
the "0" is how many seconds to wait before redirecting. You can change it to another number if you wish.
Offline
sparks wrote:
Firstly, did you google that question yet? and secondly, do you mean read the contents of a file/page at an url?
I did google it, and didn't get a proper answer. I mean read url, just like the read contents url block in panther.
Offline
sparks wrote:
I usually don't redirect in js, I just thought that's what you wanted. Php redirect is very good, nice and fast. Simply put the code
<?php header("your url"); ?> in your page. There are conditions, the redirect must come before any other php on that page, and variables cannot be included as part of the redirect URL. if you want to redirect with HTML, which is slower but does not have these limitations, use the code
<meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com">
the "0" is how many seconds to wait before redirecting. You can change it to another number if you wish.
You forgot the Location: bit in the PHP code
Besides I'm sure variables work. Have you tried
header('Location: ' . $url) before?
Offline
Oops, yes I missed location! My bad!
I've had terrible experiences with variables and php redirect in the past when I first started learning php. I wanted to do this sort of thing:
$url = 'http://www.asite.com/stuff.php?thing =' . $thing . '&otherthing=' . $otherthing;
header('Location:' . $url);
and many variations thereof such as putting the whole url and several variables into the actual header line since nothing is supposed to go before it.
Offline
hang on
Read this to run through a file line by line or character by character: http://www.w3schools.com/php/php_file.asp (I found that with google) and to read an entire file into a variable use file_get_contents()
Last edited by sparks (2011-09-11 11:06:53)
Offline
sparks wrote:
hang on
Read this to run through a file line by line or character by character: http://www.w3schools.com/php/php_file.asp (I found that with google) and to read an entire file into a variable use file_get_contents()
That's for files on a user's PC. I need it to read a url contents.
Offline
It most certainly is NOT for that!
It can be, using the file prefix as part of the URL, just like any php function dealing with a location, but it works fine with online files. I used the line-by-line file reader just a moment ago and use file_get_contents all the time!
Offline
Ok! So can you fix this up for me:
<?php
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" + username + "&password=" + password );
?>
if(<?php echo $fileContents; ?> == "false"){
method
}
else {
method
}Offline
<?php
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $username . "&password=" . $password );
?>
if(<?php echo trim($fileContents); ?> == "false"){
method
}
else {
method
?>
}
Javascript uses + to join strings, php uses periods. Javascript variables don't have a prefix, php use $. You cannot use javascript variables in php code, only the other way around so make sure you set them. You put some php outside the php tags so I moved them for you. You're probably taking them from a form using user input so $_POST['username'] and $_POST['password'] is the way to go. Finally, you need to trim the whitespace off the authenticate user page before checking or it will never return false. Since you're taking usernames and passwords, I'm going to remind you that storing either is a big, big no-no without the user giving you permission or applying a privacy statement. If you HAVE to store or send the passwords you MUST encrypt them with md5 or similar and never store or view the passwords in their unencrypted form.
Last edited by sparks (2011-09-11 11:32:35)
Offline
sparks wrote:
<?php
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $username . "&password=" . $password );
?>
if(<?php echo trim($fileContents); ?> == "false"){
method
}
else {
method
?>
}Javascript uses + to join strings, php uses periods. Javascript variables don't have a prefix, php use $. You cannot use javascript variables in php code, only the other way around so make sure you set them. You put some php outside the php tags so I moved them for you. You're probably taking them from a form using user input so $_POST['username'] and $_POST['password'] is the way to go. Finally, you need to trim the whitespace off the authenticate user page before checking or it will never return false. Since you're taking usernames and passwords, I'm going to remind you that storing either is a big, big no-no without the user giving you permission or applying a privacy statement. If you HAVE to store or send the passwords you MUST encrypt them with md5 or similar and never store or view the passwords in their unencrypted form.
Thanks! No, i'm not storing passwords.
Offline
sparks wrote:
<?php
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $username . "&password=" . $password );
?>
if(<?php echo trim($fileContents); ?> == "false"){
method
}
else {
method
?>
}Javascript uses + to join strings, php uses periods. Javascript variables don't have a prefix, php use $. You cannot use javascript variables in php code, only the other way around so make sure you set them. You put some php outside the php tags so I moved them for you. You're probably taking them from a form using user input so $_POST['username'] and $_POST['password'] is the way to go. Finally, you need to trim the whitespace off the authenticate user page before checking or it will never return false. Since you're taking usernames and passwords, I'm going to remind you that storing either is a big, big no-no without the user giving you permission or applying a privacy statement. If you HAVE to store or send the passwords you MUST encrypt them with md5 or similar and never store or view the passwords in their unencrypted form.
I got that, but can you help me make the form?
Offline
sparks wrote:
... it's not hard, w3 schools has an entire section. Why not give it a go and I'll give you a hand if you get stuck, you'll not learn much if I just tell you everything
![]()
lol, kk!
Offline
sparks wrote:
... it's not hard, w3 schools has an entire section. Why not give it a go and I'll give you a hand if you get stuck, you'll not learn much if I just tell you everything
![]()
I have this code, but it won't work:
<title>Scratch User Panel 1.0</title>
<form action="" method="post">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" />
</form>
<title>Scratch User Panel 1.0</title>
<script language="javascript" type="text/javascript" src="http://plaxon.org/js_methods.js"></script>
<?php
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $username . "&password=" . $password );
?>
if(<?php echo trim($fileContents); ?> == "false"){
Invalid Username/Password.
}
else {
</script>
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter a username:");
var projectid=prompt("Please enter a project ID:");
if (projectid!=null && projectid!="")
{
window.location = "http://scratch.mit.edu/projects/" + name + "/" + projectid;
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Open a Project" />
</body>
</html>
?>
}Offline
<title>Scratch User Panel 1.0</title>
<?php
if(!isset($_POST['submit'])){ ?> #do this if form hasn't been submitted
<form action="thisPage'sUrlHere.php" method="post" name="loginForm">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name = "submit" value = "Whatever you want the button to say"/>
</form>
<title>Scratch User Panel 1.0</title> (remove)
<script language="javascript" type="text/javascript" src="http://plaxon.org/js_methods.js"></script>
<?php
} #end of !isset post submit
if(isset($_POST['submit'])){ #do this if form is submitted
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'] );
?>
if(<?php echo trim($fileContents); ?> == "false"){
Invalid Username/Password.
}
else {
</script>
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter a username:");
var projectid=prompt("Please enter a project ID:");
if (projectid!=null && projectid!="")
{
window.location = "http://scratch.mit.edu/projects/" + name + "/" + projectid;
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Open a Project" />
</body>
</html>
?> #<--- why is this here with a } below it? move it down!
}
} #end of isset post submit
} #the one you put up there which is now grey
?>
I haven't checked your javascript for you but I think that php should be okay. Learn from what you see ;) Always name form elements including the submit button, see how the submit button being set or not determines the content of the page!
Offline
sparks wrote:
<title>Scratch User Panel 1.0</title>
<?php
if(!isset($_POST['submit'])){ ?> #do this if form hasn't been submitted
<form action="thisPage'sUrlHere.php" method="post" name="loginForm">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name = "submit" value = "Whatever you want the button to say"/>
</form>
<title>Scratch User Panel 1.0</title> (remove)
<script language="javascript" type="text/javascript" src="http://plaxon.org/js_methods.js"></script>
<?php
} #end of !isset post submit
if(isset($_POST['submit'])){ #do this if form is submitted
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'] );
?>
if(<?php echo trim($fileContents); ?> == "false"){
Invalid Username/Password.
}
else {
</script>
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter a username:");
var projectid=prompt("Please enter a project ID:");
if (projectid!=null && projectid!="")
{
window.location = "http://scratch.mit.edu/projects/" + name + "/" + projectid;
}
}
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Open a Project" />
</body>
</html>
?> #<--- why is this here with a } below it? move it down!
}
} #end of isset post submit
} #the one you put up there which is now grey
?>I haven't checked your javascript for you but I think that php should be okay. Learn from what you see ;) Always name form elements including the submit button, see how the submit button being set or not determines the content of the page!
Thank you!
Last edited by WindowsExplorer (2011-09-11 15:56:49)
Offline
It still won't work. Heres' what I have:
<title>Scratch User Panel 1.0</title>
<?php
if(!isset($_POST['submit'])){ ?>
<form action="scratch_cp.php" method="post" name="loginForm">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name = "submit" value = "Login as . $username "/>
</form>
<script language="javascript" type="text/javascript" src="http://plaxon.org/js_methods.js">
function show_prompt()
{
var name=prompt("Please enter a username:");
var projectid=prompt("Please enter a project ID:");
if (projectid!=null && projectid!="")
{
window.location = "http://scratch.mit.edu/projects/" + name + "/" + projectid;
}
</script>
<?php
}
if(isset($_POST['submit'])){
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'] );
?>
if(<?php echo trim($fileContents); ?> == "false"){
Invalid Username/Password.
}
else {
</script>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Open a Project" />
</body>
</html>
}
?>Offline
Right, I'm looking through it in notepad++ which is colour coding it all for me. Watch this space:
Firstly, you have more { than } in your js show_prompt() function.
Secondly you have an if statement OUTSIDE a php tag set.
You need to echo the text "invalid username/password"
You have a </script> tag without a <script> tag
You're missing a } at the end of the page
You're trying to send text (username and password invalid message) outside of the html tags
<title>Scratch User Panel 1.0</title>
<?php
if(!isset($_POST['submit'])){ ?>
<form action="scratch_cp.php" method="post" name="loginForm">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name = "submit" value = "Login as <?php echo $username;?>" />
</form>
<script language="javascript" type="text/javascript" src="http://plaxon.org/js_methods.js">
function show_prompt(){
var name=prompt("Please enter a username:");
var projectid=prompt("Please enter a project ID:");
if (projectid!=null && projectid!=""){
window.location = "http://scratch.mit.edu/projects/" + name + "/" + projectid;
}
}
</script>
<html>
<head>
<?php
}
if(isset($_POST['submit'])){
$fileContents = file_get_contents("http://scratch.mit.edu/api/authenticateuser?username=" . $_POST['username'] . "&password=" . $_POST['password'] );
if(trim($fileContents) == "false"){
echo "Invalid Username/Password.";
}
else { ?>
<script type="text/javascript">
</script>
</head>
<body>
<input type="button" onclick="show_prompt()" value="Open a Project" />
</body>
</html>
<?php
}
}
?>You have an empty script tag near the bottom which I've left if case you want it there. I've also tidied up your layout a bit using tab to make it easier to read and follow
Something to get into the habit of is writing comments all over your code so you don't forget what bits do! Look up php/HTML/ javascript comments, they all do it differently but they're very useful!
Oh, update, you made a mistake by putting a variable in speech marks for the "log in as $username" button. By the way, $username is unset in the code when that line is reached so the button is always going to say "Log in as"
Last edited by sparks (2011-09-11 17:27:09)
Offline