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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect from form?!

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
I've got a login form on my homepage, default.asp, that has a username input, password input, and the submit button. I submit to an asp page called verifyLogin.asp...
From the top of the form:

Response.Write &quot;<form method='POST' action='verifyLogin.asp'>&quot;


From verifyLogin, I've got:
<% response.buffer = true %>
at the top, and then lower in the page, it logs the user and is supposed to redirect back to the hone page, default.asp, like so:

Response.redirect &quot;default.asp&quot;

but it doesn't redirect. It gives me error


Response object error 'ASP 0156 : 80004005'

Header Error

/verifyLogin.asp, line 50

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.



I'm new at this ASP thing and I have no idea how to fix this. I haven't changed the header, it just goes right back to the homepage, no extra header information, no nothing. What's the problem??

Thanks a lot... Cyprus
 
can you post all of your code for the verifylogin.asp page?

Maybe someone will be able to pick it out easier...

Thanks,
mwa
 
header modifications must be followed by a response.clear and ended with a response.end
so try this instead

Response.Clear
Response.Redirect &quot;default.asp&quot;
Response.End ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

 
that should follow a not followed ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

 
Make sure:

<%@ LANGUAGE=&quot;VBSCRIPT&quot; %>
<% Option Explicit %>

<% response.buffer = true %>

Appears before ANY HTML code in an ASP page.

Generally these are always at the top.

 
here's my code... and putting redirect between response.clear and response.end didn't work. also, if I use option explicit I get a 500 server error. I assume I put in a variable wrong or something but I'm not experienced enough to know... THANKS!

here's the chunk of code for...
default.asp
<%
if Session.Contents(&quot;Logged&quot;) = true Then
Response.Write(&quot;Logged in as <b>&quot; & Session.Contents(&quot;UserName&quot;) & &quot;</b><br>&quot;)
Response.Write(&quot;<a href='logout.asp'>Log out</a>&quot;)
else
Response.Write &quot;<form method='POST' action='verifyLogin.asp'> &quot;
Response.Write &quot;<h4>Sign in</h4>&quot;
Response.Write &quot;<p>User Name: <input type='text' name='UsrName' size='20'></p>&quot;
Response.Write &quot;<p>Password:   <input type='password' name='Password' size='20'></p>&quot;
Response.Write &quot;<p><input type='checkbox' name='SaveLogin' value='ON'>Remember me</p>&quot;
Response.Write &quot;<p><input type='submit' value=' OK ' name='B1'></p>&quot;
Response.Write &quot;</form>&quot;
end if
%>

...and all of
verifylogin.asp
<%
response.buffer = true

Dim strUserName
Dim bLoginSaved

if Request.Form(&quot;SaveLogin&quot;) = &quot;ON&quot; Then
if Request.Form(&quot;UsrName&quot;) = &quot;&quot; or request.Form(&quot;Password&quot;) = &quot;&quot; then
Session.Abandon
Response.Clear
Response.Redirect &quot;default.asp&quot;
else
Response.Cookies(&quot;SavedLogin&quot;)(&quot;UserName&quot;) = Request.Form(&quot;UsrName&quot;)
Response.Cookies(&quot;SavedLogin&quot;)(&quot;Pass&quot;) = Request.Form(&quot;Password&quot;)
Response.Cookies(&quot;SavedLogin&quot;).Expires = Date + 30
bLoginSaved = true
strUserName = Request.Form(&quot;UsrName&quot;)
end if
else
bLoginSaved = false
end if
%>
<html>

<head>
<title>Verify Login</title>
</head>

<body>
<h2>Login</h2>
<p> </p>
<%
if bLoginSaved then
Response.Write &quot;Saving login information...<br>&quot;
Response.Flush
Session(&quot;Logged&quot;) = true
Session(&quot;UserName&quot;) = strUserName
Response.Write &quot;<br>You have opted to login automatically. A cookie has been created that will automatically log you into your name<br>&quot;
Response.Write &quot;<A href='default.asp'>Click here</a> to continue&quot;
else
Response.Write &quot;Logging in...<br>&quot;
Response.Flush
Session(&quot;Logged&quot;) = true
Session(&quot;UserName&quot;) = strUserName
Response.Clear
Response.Redirect Request(&quot;dest&quot;)
Response.End
end if
%>
</body>

</html>
Cyprus
 
Set the Request(&quot;dest&quot;) to a variable at the top then use that in the redirect.


Response.Redirect strDest


 
Didn't work. Gives me the same error as before. But now I've got a middleman. I tried just using &quot;default.asp&quot; in the Response.Redirect but got more of the same. Maybe this'll help, Here's the rest of default.asp


<%
response.buffer = true

if request.Cookies(&quot;SavedLogin&quot;).HasKeys then
Session.Contents(&quot;Logged&quot;) = true
Session.Contents(&quot;UserName&quot;) = request.Cookies(&quot;SavedLogin&quot;)(&quot;UserName&quot;)
else
Session.Contents(&quot;Logged&quot;) = false
end if
%>
<html>

<head>
</head>
<body>
<%
if Session.Contents(&quot;Logged&quot;) = true Then
Response.Write(&quot;Logged in as <b>&quot; & Session.Contents(&quot;UserName&quot;) & &quot;</b><br>&quot;)
Response.Write(&quot;<a href='logout.asp'>Log out</a>&quot;)
else
Response.Write &quot;<form method='POST' action='verifyLogin.asp?dest=default.asp'> &quot;
Response.Write &quot;<h4>Sign in</h4>&quot;
Response.Write &quot;<p>User Name: <input type='text' name='UsrName' size='20'></p>&quot;
Response.Write &quot;<p>Password:   <input type='password' name='Password' size='20'></p>&quot;
Response.Write &quot;<p><input type='checkbox' name='SaveLogin' value='ON'>Remember me</p>&quot;
Response.Write &quot;<p><input type='submit' value=' OK ' name='B1'></p>&quot;
Response.Write &quot;</form>&quot;
end if
%>
</body> Cyprus
 
Session.Contents() is a collection

ie: Session.Contents(1) is proper syntax.

Maybe you meant:

Session(&quot;Logged&quot;)

Not

Session.Contents(&quot;Logged&quot;)
 
ok... You've succeeded in confusing me. lol.

Could I use Server.Transfer to get it back to the home page? That seemingly works. Cyprus
 
Can you show your logout.asp page.

I recreated both of these pages without problem.

 
hum... And you could log in and it would go to the page and automatically come back? My logout page works fine... Cyprus
 
Logout page...

<% response.buffer = true %>
<html>

<head>
<title>Logout</title>
</head>

<body>
<h3>Logout</h3>
<p>&nbsp;</p>
<%
Response.Cookies(&quot;SavedLogin&quot;).Expires = Date - 20
Response.Cookies(&quot;SavedLogin&quot;)(&quot;UserName&quot;) = &quot;&quot;
Session.Abandon
Response.Redirect(&quot;default.asp&quot;)
%>
</body>

</html>
Cyprus
 
Comment out the Flush command, verifylogin.asp.

else
Response.Write &quot;Logging in...<br>&quot;
'Response.Flush
Session(&quot;Logged&quot;) = true
Session(&quot;UserName&quot;) = strUserName
Response.Clear
Response.Redirect &quot;default.asp&quot;
Response.End
end if

Also if save is not checked how does strUserName get set?
I added:
strUserName = Request.Form(&quot;UsrName&quot;)
to the top of the page.


 
actually, I think I did the same thing. Added the name, I mean. I don't know why it's not in there. I thought I had done that a while back.

OK, the problem is not redirecting back to the main form. The problem is somehow with my verifyLogin page... It won't let me redirect to ANYTHING. What could be my problem?? If I just add a link to the page and go to it like that, I can redirect just fine. I'm sure it has something to do with going to that site through submit or something. Any help!?!

This has got me snagged way too long... Cyprus
 
Are you sure you don't have any other code on that Page?

When I first ran it I only got an error when the save box was NOT checked After I commeted out the FLush everything worked fine.

Hmmm . . .
 
Ok, first problem, I don't actually see any verificaion occuring unless you count verifying it against the last user name they entered if they chose to save the name. Thus a blank username and password allow you login in so long as you don't choose to be remembered.

Second, the problem you had above was mis diagnosed, the error is occuring because you must write your cookies before nything is output to the user, once you start to output to the user than you hacve already sent them the file header and can't change the contents, like you would need to do to write a cookie.

Next, you may want to try and structure this script more like this:
Code:
<% 
	Option Explicit 
    
	Dim strUserName    
	Dim bLoginSaved

	If Request.Form(&quot;UsrName&quot;) = &quot;&quot;  or request.Form(&quot;Password&quot;) = &quot;&quot; then
		Response.Redirect &quot;default.asp&quot;
	End If

	'do whatever you need to verify them here----
	Dim verificationPassed
	verificationPassed = true 'auto pass everyone for test purposes
	'--------------------------------------------


	If NOT verificationPassed Then
		Response.Redirect &quot;default.asp&quot;
	End If
		


    if Request.Form(&quot;SaveLogin&quot;) = &quot;ON&quot; then
		'Write the cookie
		Response.Cookies(&quot;SavedLogin&quot;)(&quot;UserName&quot;) = Request.Form(&quot;UsrName&quot;)
		Response.Cookies(&quot;SavedLogin&quot;)(&quot;Pass&quot;) = Request.Form(&quot;Password&quot;)    
		Response.Cookies(&quot;SavedLogin&quot;).Expires = Date + 30		%>
		<html>

		<head>
		<title>Verify Login</title>
		</head>

		<body>
		<h2>Login</h2>
		<p> </p>
		<%
        Response.Write &quot;Saving login information...<br>&quot;
        Response.Flush
        Session(&quot;Logged&quot;) = true
        Session(&quot;UserName&quot;) = strUserName
        Response.Write &quot;<br>You have opted to login automatically. A cookie has been created that will automatically log you into your name<br>&quot;
		
		bLoginSaved = true
		strUserName = Request.Form(&quot;UsrName&quot;)

        Response.Write &quot;<A href='default.asp'>Click here</a> to continue&quot;        
    else
        Response.Write &quot;Logging in...<br>&quot;
        Response.Flush
        Session(&quot;Logged&quot;) = true
        Session(&quot;UserName&quot;) = strUserName
'        Response.Clear
'        Response.Redirect Request(&quot;dest&quot;)
		Response.Write &quot;ColorChooser.asp&quot;
'        Response.End 
    end if
%>
</body>

</html>

Let me know how that works for you,

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
No more vacation for me :(

FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top