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

PHP redirect function

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
In Active Server Pages, there is a function called response.redirect and what it does is that when it hits that particular line of code it "redirects" the viewer to a page that you passed to the function.
ex.
If (login == correct)
response.redirect("coninue.asp")
else
response.redirect("error.asp")
end if

is there such a funciton in PHP. I can't seem to find anything on the web.

Also, using MySQL, after creating atable, is it possible to add columns/fields. In MS Access, you just go to design view and create the new column, but what is the syntax if it is possible in MySQL.
Thank YOu
 
header( "Location: somwhereOnTheNet.php" )

At first you may experience some odd errors, such as:
"cannot send headers, headers already sent..."

That means you've got some stray space or html or something before you call header().

More info on header can be found at:
 
You can get around the header not sent errors by adding
ob_start();

at the beginning of your pages.

ob_start() eq Response.buffer = true

see the output control section of the manual for more details (like how to flush, and how to end).
leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
ob_start is a nice solution, but you need an ob_end_flush in the end, cause without it you don't write anything in the screen:

if (something happens) Header("Location: somewhereelse");
else{
echo "bla bla bla";
}
you need to finish the script with ob_end_flush(); Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thank you all. Is this the only possible way? And if so what does ob_start and ob_end mean and what does it do. Why does the header function produce the errors.

Thanks in Advanced
 
----------
Also, using MySQL, after creating atable, is it possible to add columns/fields. In MS Access, you just go to design view and create the new column, but what is the syntax if it is possible in MySQL.
Thank YOu
----------

To add a field, use ALTER TABLE.

Lets say you create a table called your_table, with a CHAR field:
mysql>CREATE TABLE your_table (field2 CHAR);

mysql>desc your_table;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| field2 | char(1) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+

The following example creates an INT field and inserts it FIRST, before all other fields, then a CHAR at the end, then a VARCHAR after an existing field2:

mysql>ALTER TABLE your_table ADD field1 INT NOT NULL FIRST, ADD field4 CHAR NOT NULL, field3 VARCHAR(20) NULL AFTER field2;

If you then desc your_table, you should see the following:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| field1 | int(11) | | | 0 | |
| field2 | char(1) | YES | | NULL | |
| field3 | varchar(20) | YES | | NULL | |
| field4 | char(1) | | | | |
+--------+-------------+------+-----+---------+-------+


Also see the following:
Erin Quick-Laughlin
DBA, PHP/MySQL programmer
 
hi
if u want to redirect the user to some other location using header function u have to make sure that there is no output sent to the browser before u use the header function.
so there shd be no space,newline character or anyother text(html or php)
before ur first php tag ie <?php

in ob_start function the output is stored in an internal buffer instead of displaying it to the browser and ob_end_flush() function flushes the output buffer to the browser
so u can send header like this
<?php
ob_start();
echo &quot;some output here&quot; ;
if(condition)
header(&quot;location...&quot;)
else
echo &quot;else part&quot; ;
ob_end_flush();
?>
so no output will be sent to the browser untill ob_end_flush() causing header location to work

hope that clears ur doubts a little

cheers
spookie

 
An easy way of redirecting anywhere in a PHP script is to use JavaScript.

Something like this will do the trick:
<?PHP
if ($fail)
{
?>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>location.replace('yourpage.php');<SCRIPT>
<?PHP
}
?>

That does it. The current page will even dissapear from the history. If you don't want this to happen use location.reload('') in stead.

Notice that I switch from PHP to ordinary HTML. You can also echo the HTML from within the PHP code, facilitating passing of varaibles etc.

Re. your MYSQL question: get PHPMyAdmin, whch is an excellent MySQL database design and maintenance system written in PHP. Get it at
Martin
 
Thank You. Once again tek-tips has helped me through another tough issue. Thank You guys. You are all very helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top