|
-
May 10th, 2006, 10:24 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Redirecting using meta tags.
Is it advisable to redirect a page using meta tags instead of headers?
What are the pros and cons in using meta tags in redirecting a page assuming you have a session in your page?
Thanks in advance for all of the inputs.
-
May 11th, 2006, 01:00 AM
#2
Re: Redirecting using meta tags.
If you redirect using headers or by using the location header in the meta tag, the browser does not cache the page. Clicking the back button will cause the page to go back to the original page.
If you use a refresh header with a timeout the browser will cache the page.
It is often better to send the headers in the HTTP response as the client is not required to interpret headers inside meta tags. Although the majority do.
-
May 11th, 2006, 02:23 AM
#3
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
Vis what about those 301 redirect? Is it the header redirection?
header("location: mypage.php");
I've used meta tags (<meta http-equiv='refresh' content='5;url=$url'>) for a certain reason because when I used header function it will prompt me an error bec. there were some output send which violates the rule in using the header function. Does my page will be messy coz I always used the meta tags? I don't know what will happend in the long run using the meta tags.
-
May 11th, 2006, 03:59 AM
#4
Re: Redirecting using meta tags.
301:
PHP Code:
header('HTTP/1.1 301 Moved Permanently');
header('location: mypage.php');
I would advise against meta redirects unless you need a timed redirect. As for the problems sending headers, it's up to you to place them correctly
-
May 11th, 2006, 04:13 AM
#5
Re: Redirecting using meta tags.
 Originally Posted by mar_zim
Vis what about those 301 redirect? Is it the header redirection?
header("location: mypage.php");
I've used meta tags (<meta http-equiv='refresh' content='5;url=$url'>) for a certain reason because when I used header function it will prompt me an error bec. there were some output send which violates the rule in using the header function. Does my page will be messy coz I always used the meta tags? I don't know what will happend in the long run using the meta tags.
Your problem with output can be solved easily. Code your scripts better. Ideally you want all you output to occur after processing. If you really can't rewrite the code, start an output buffer in the first line of the script using:
Like I said already. Use a header to make redriects as the client is not required to act apon the meta tags. With a HTTP redirect, the status code is changed to 3xx, whereas when using a meta tag the status code will be 200 and you run into the caching problems I mentioned earlier.
Last edited by visualAd; May 11th, 2006 at 04:21 AM.
-
May 11th, 2006, 04:32 AM
#6
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
This way I used meta tags.
PHP Code:
if(!(isset($_POST['name']))){
echo"<table>
<tr><td><img src='images/newheader1.jpg'></td></tr>
</table><hr>
<link rel='stylesheet' type='text/css' href='mystyle.css'>
<br><br><br>
<center>
<table border='1' cellpadding='3' cellspacing='0'>
<tr class='bold'><td>There is no page in this address.</td></tr></table></center>";
$url='log.php';
echo "<meta http-equiv='refresh' content='5;URL=$url'>";
return;
}
and this way in my confirm page.
PHP Code:
$query="select empid from table1 where name='$name' and password='$pass'";
$r=mssql_query($query);
$empid=mssql_fetch_row($r);
$db->myclose();
$_SESSION['id']=$empid[0];
if(mssql_num_rows($r)==1){
echo "Loading...";
$url='displaydtr.php?action=show';
echo "<meta http-equiv='refresh' content='1;URL=$url'>";
return;
}
else{
echo"<table>
<tr><td><img src='images/newheader1.jpg'></td></tr>
</table><hr>
<link rel='stylesheet' type='text/css' href='mystyle.css'>
<br><br><br>
<center>
<table border='1' cellpadding='3' cellspacing='0'>
<tr class='bold'><td>User name or password is incorrect!</td></tr></table></center>";
}
What would be the best way in doing that in using headers and not the meta tags?
Sorry for asking such a n00bz question I'm really new at this thingy.
thanks in advance.
-
May 11th, 2006, 04:49 AM
#7
Re: Redirecting using meta tags.
First read this post. Then slap yourself for writing such code 
Ideally you want to do something like this:
- Execute your query.
- Check the number of rows and send a header if ncessary, storing the reuslt in a variable.
- Output the pageu using valid HTML.
PHP Code:
<?php
if(mssql_num_rows($r)==1) {
$url='displaydtr.php?action=show';
header("Location: $url");
exit;
}
?>
<html>
<head>
<title>Title</title>
<!-- note: style sheet links go in the header -->
<link rel='stylesheet' type='text/css' href='mystyle.css'>
</head>
<body>
<table>
<tr>
<td><img src='images/newheader1.jpg'></td>
</tr>
</table>
<hr />
<br /><br /><br />
<center>
<table border="1" cellpadding="3" cellspacing="0">
<tr class="bold">
<td>User name or password is incorrect!</td>
</tr>
</table>
</center>
</body>
</html>
-
May 11th, 2006, 04:51 AM
#8
Re: Redirecting using meta tags.
<hr /> and <br /> aren't valid HTML 
And don't use tables for layout!
-
May 11th, 2006, 07:59 PM
#9
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
Sorry adam for writing such messy code it's just happend I came from .Net realm(windows forms specifically and my friend told me just use asp.net instead of php and my answer was...ahh..hmmmnn..maybe next time ) and decided to jump to web applications using php and I have a very limited knowledge in html and so called css. Don't worry time will come you would see me naked running around shouting "adam from Slough teach me how to wear swimming trunks". 
Ok I will just slap my self some other time and finish my first application in web. I just post back for the up coming results. Thank you very much adam for such a great help.
 Originally Posted by penagate
<hr /> and <br /> aren't valid HTML
And don't use tables for layout!
So what would be the valid then? and why not tables?
-
May 11th, 2006, 08:32 PM
#10
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
Now here's the error guys. Again it pointing to my connectmssql.php file in which I establish the connect function.
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\connectmssql.php:29) in C:\Program Files\Apache Group\Apache2\htdocs\confirm.php on line 38
Here's the entire code in connectmssql.php
PHP Code:
<?php
class DBConnect{
private $host;
private $user;
private $pw;
private $link;
public function myconnect($host,$user,$pw)
{
$this->host=$host;
$this->user=$user;
$this->pw=$pw;
$this->link=mssql_connect($this->host,$this->user,$this->pw);
if(!$this->link)
{
die('could not connect'.mssql_error());
}
}
public function myclose()
{
mssql_close($this->link);
}
}
?>
and here's what I'm using it.
PHP Code:
<?php
session_start();
$_SESSION['name']=$_POST['name'];
require 'connectmssql.php';
$db=new DBConnect;
$db->myconnect('xxx','sa','password');
$name=$_POST['name'];
$pass=$_POST['pass'];
mssql_select_db('mydb');
$query="select empid from dtrlog where name='$name' and password='$pass'";
$r=mssql_query($query);
$empid=mssql_fetch_row($r);
$db->myclose();
$_SESSION['id']=$empid[0];
if(mssql_num_rows($r)==1){
echo "Loading...";
$url='displaydtr.php?action=show';
header("Location: $url");
return;
}
?>
<html>
<head><title>Confirm Log</title>
<link rel='stylesheet' type='text/css' href='mystyle.css'>
</head>
<table>
<tr><td><img src='images/newheader1.jpg'></td></tr>
</table><hr /><br /> <br /><br />
<center>
<table border='1' cellpadding='3' cellspacing='0'>
<tr class='bold'><td>User name or password is incorrect!</td>
</tr><table></center>
</html>
What could be the reason why it throws such exception? I don't know where it sends an output. Again thank you for further assistance.
-
May 11th, 2006, 09:13 PM
#11
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
My option is that I comment my connect class and call mssql_connect function in the page.
PHP Code:
<?php
session_start();
$_SESSION['name']=$_POST['name'];
#require 'connectmssql.php';
#$db=new DBConnect;
#$db->myconnect('dtr','sa','closer');
mssql_connect('xxx','sa','password');
$name=$_POST['name'];
$pass=$_POST['pass'];
mssql_select_db('hps');
$query="select empid from dtrlog where name='$name' and password='$pass'";
$r=mssql_query($query);
$empid=mssql_fetch_row($r);
$_SESSION['id']=$empid[0];
if(mssql_num_rows($r)==1){
#echo "Loading...";
$url='displaydtr.php?action=show';
header("Location: $url");
return;
}
#$db->myclose();
?>
<html>
<head><title>Confirm Log</title>
<link rel='stylesheet' type='text/css' href='mystyle.css'>
</head>
<table>
<tr><td><img src='images/newheader1.jpg'></td></tr>
</table><hr /><br /> <br /><br />
<center>
<table border='1' cellpadding='3' cellspacing='0'>
<tr class='bold'><td>User name or password is incorrect!</td>
</tr><table></center>
</html>
Now it works and still I don't know the reason why it throws an error when I use my connection class.
-
May 12th, 2006, 02:11 AM
#12
Re: Redirecting using meta tags.
Headers by there nature go on top. If you look at an HTTP response (you can see what they look like by downloading the HTTP VB app in my sig), the headers come before the body of the request which contains the data.
As soon as you send any output , that is and data which is not inside PHP tags or any data output using echo or print. The headers are flucshed and sent to the client. Once that has happened you cannot send any more headers.
Your error is caused becuase immdiately before you send your header, you have used echo:
PHP Code:
if(mssql_num_rows($r)==1){
echo "Loading..."; // <------ this is bad
$url='displaydtr.php?action=show';
header("Location: $url");
return;
}
To eliminate the error, make sure there is no output before you send the header. The connection class has nothing to do with it.
-
May 12th, 2006, 02:17 AM
#13
Re: Redirecting using meta tags.
 Originally Posted by mar_zim
Sorry adam for writing such messy code it's just happend I came from .Net realm(windows forms specifically and my friend told me just use asp.net instead of php and my answer was...ahh..hmmmnn..maybe next time  ) and decided to jump to web applications using php and I have a very limited knowledge in html and so called css. Don't worry time will come you would see me naked running around shouting "adam from Slough teach me how to wear swimming trunks".
Ok I will just slap my self some other time and finish my first application in web. I just post back for the up coming results. Thank you very much adam for such a great help.
So what would be the valid then? and why not tables?
The purpose of a markup language is to make the data that it marks up have meaning to both computers and humans. Tables are designed to display tabulated data not position objects ona page. Using tables to control layout make your code look messy; destroy its readbiliity and make it very difficult to update.
Have a look at CSS. Sites like www.csszengarden.com show how you can acheive equal results without a table in sight. Whats more, changing the design is as simple as slaping another style sheet on top of the source code. 
Penagate is right,<br /> is not valid HTML It should be <br>. In XHTML the former is required however, it indicates a opening and closing tag all in one.
-
May 12th, 2006, 02:42 AM
#14
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
 Originally Posted by adam
The connection class has nothing to do with it.
I tried commenting the echo part but still it throws an error pointing to the connection class.
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\connectmssql.php:29) in C:\Program Files\Apache Group\Apache2\htdocs\confirm.php on line 27
PHP Code:
if(mssql_num_rows($r)==1){
#echo "Loading...";
$url='displaydtr.php?action=show';
header("Location: $url");
return;
}
Now I feel that I'm soo annoying.
-
May 12th, 2006, 03:14 AM
#15
Re: Redirecting using meta tags.
Make sure there are no blank lines before ot after the opening and closing PHP tags.
-
May 12th, 2006, 04:31 AM
#16
Thread Starter
Frenzied Member
Re: Redirecting using meta tags.
 Originally Posted by visualAd
Make sure there are no blank lines before ot after the opening and closing PHP tags.
Holy cow...there are 2 extra lines and I removed it now it works...
Thanks adam.
Now I can sleep tight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|