Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to redirect url with username/password in perl? 3

Status
Not open for further replies.

alan123

MIS
Oct 17, 2002
149
US
Hi,

I have a question regarding redirect url in perl.
I can use this code to redirect url properly:
====
#!/usr/bin/perl
print "Content-type:application/x-download\n";
$url="print "Location: $url \n\n";
====

it works fine, now I create a folder with .htaccess protected, I have setup username/password in .htpasswd, the url " can reach that protected page without prompt me the box to login, I want to write it in perl so it will login automatically.

However if I try:
====
#!/usr/bin/perl
print "Content-type:application/x-download\n";
$url="print "Location: $url \n\n";
====

It doesn't work and prompt me to enter login info. can anyone help me is it possible to redirect username/password embeded url in perl? thanks in advance.
 
sorry but I always find this sort of thing easier with some real information - can u tell us the domain?


Kind Regards
Duncan
 
Did you try running the perl script manually and make sure that what's printed out is exactly what you expect?
 
I run the script on linux command line, it has no error:
Code:
Location:[URL unfurl="true"]http://username:password@www.domain.com/protect/[/URL]

this is the original script:
Code:
#!/usr/bin/perl
$url='[URL unfurl="true"]http://username:password@www.domain.com/protect/';[/URL]
print "Location: $url \n\n";

if I directly type on browser:
Code:
[URL unfurl="true"]http://username:password@www.domain.com/protect/[/URL]
it works without prompting me the login box, but if I use this script, it prompts me the login box. how can I solve this issue?
 
alan123 - I have exactly the same problem, did you get a solution or did you go about it another way?
 
Have a look at
The username is 'abc'
The password is '123'

Duncan - I had a go with your code and couldn't get it to work. I'm using the latest version of Apache.

The idea is that multiple people will enter their username and password and then get taken to their own private page.

Thanks
 
thank you Nutthick - I know it appears nosey but it is a heck of alot easier with some REAL information! thanks to you I have an answer...

#!/usr/bin/perl

print "Content-type: application/x-download\n";

$url = "
print "Location: $url\n\n";


it was the [red]@[/red] sign!


Kind Regards
Duncan
 
Thanks Duncan, I getting closer. If I do a script with just what you gave me it works, but only in Opera. Internet Explorer gives me an error that is cannot find the server. I pasted your code into mine which I've put below, as when I use this IE gives the same error, and Opera then prompts me for a username and password again, which I think is what the original post was having problems with. Thanks for your help so far,

#!/usr/bin/perl

# Read in the form data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

open (PASSWORDS, "C:/Program Files/Apache Group/Apache2/bin/passwd/sulsado_passwords.txt");

while ($Temp = <PASSWORDS>)
{
# Get the Values
$Username = $Temp;
$Username = trim($Username);

$Password = <PASSWORDS>;
$Password = trim($Password);
$Link = <PASSWORDS>;
$Link = trim($Link);

# Check Username and Password
if (($FORM{'txtUser'} eq $Username) && ($FORM{'txtPassword'} eq $Password))
{
print "Content-type: application/x-download\n";

$url = "
print "Location: $url\n\n";
}
}

close(PASSWORDS);

print "Content-type: text/html\n\n";
print "<HTML><HEAD>";
print "<TITLE>CGI Test</TITLE>";
print "</HEAD>";
print "<BODY><H2>The Values are - *";
print "Denied";
print "*</H2></BODY></HTML>";

# Removes leading/trailing spaces
sub trim {
my @out = @_;
for (@out) {
s/^\s+//;
s/\s+$//;
}
return wantarray ? @out : $out[0];
}
 
I know it seems trivial but try changing this line:-

print "Content-type: application/x-download\n[red]\n[/red]";

... as far as I am aware this is VERY important!


Kind Regards
Duncan
 
No luck, putting in the extra \n causes the script to try and download itself. I get the a download dialog popping up asking me where I want to save the script that I'm running. Strange
 
can you give me a sample $buffer value? I might be able to determine what is happening from this...


Kind Regards
Duncan
 
you musn't print "Content-type:".
only print "Location: ..."

---------------------------------------
If you helped... thx.
 
I could be wrong, but AFAIK the http://username:password@example.com convention is a browser client feature - it's nothing to do with http auth. It simply gives information for the client to send in the appropriate manner.

You want to read RFC2617 which is all about HTTP auth.

also remember that the browser passes the info to the server in base64, so you'll need to encode the info for a perl script to pick it up.

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 
duncdude - txtUser=abc&txtPassword=123&Submit=Submit All the data is coming in OK as it is checked with a text file before performing a redirection. The text file contains a series of three lines. Line 1 - Username, Line 2 - Password, Line 3 - URL for successful authentication.

thenewa2x - Tried removing the first line, but it made no difference, still got the same error.

manarth - Read the RFC, still in the dark though. Will have another go at it later as was in a hurry.
 
apologies for my somewhat confusing post earlier; I was under the impression you wanted the perl script to login to a server (in which case the perl script needed to encode the username/password info).

actually, you wanted to pass the login info to a user's browser - in which case duncdude's code seems to be doing the job.

just to clarify duncdude, what browser are you using?
(not all support the username:password@ method)

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 
not offence taken manarth - I think i'm starting to lose the plot... I thought it had worked because it stripped out the username:password and turned:-

[red][/red]

into:-

[blue][/blue]

... therefore, stupidly, I assumed it had worked!

I find it very hard to work on problems without any real data - until Nutthick arrived on this post - but when I visit it doesn't even ask me for a login / password???


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top