redirecting pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

redirecting pages

Post by gavinbsocom »

I just switched over from learning asp to learning php. And I need to know if i can use if,then, else.....and if someone could give me an example please. Also, how do i redirect something like in asp. if this = this then. response.redirect here. How do i do that i php....? anyone understand what im saying?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Take a look to control-structures and header sections of the PHP Manual. Also read the Warning: Cannot add header information tutorial.

Here is a simple code:

Code: Select all

<?
$redirect = 'Ok';
if ($redirect == 'Ok')
	header('Location: page1.php');
else
	header('Location: page2.php');
?>
Regards,
Scorphus.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

is $redirect one of those set function in php? and if so how would i say


if $_POST["name"] = "gavin";
if $POST["pass"] = "socom";
$redirect = "login.php";
else $redirect = "nologin.php";
end if


is this something that looks right? im not even sure...thanks...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

In scorphus example $redirect was a variable.

The code you have written, re-written correctly in PHP would look like this

Code: Select all

<?

if ($_POST["name"] == "gavin" && $_POST["pass"] == "socom") {
	header('Location: login.php');
} else {
	header('Location: nologin.php');
}

?>

Glad you saw the light and swithched to PHP

Mark
Last edited by JayBird on Wed Oct 01, 2003 9:31 am, edited 1 time in total.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

No! Everything after a $ in PHP is a variable. So this:

Code: Select all

<?
$pi = 3.14159265359;
?>
is the same as (ASP):

Code: Select all

&lt;%
Dim pi
pi = 3.14159265359
%&gt;
I would say:

Code: Select all

<?
if ($_POST["name"] == "gavin" and $_POST["pass"] == "socom")
    header("Location: login.php");
else
    header("Location: nologin.php");
?>
or:

Code: Select all

<?
if ($_POST["name"] == "gavin" and $_POST["pass"] == "socom")
    $redirPage = "login.php";
else
    $redirPage = "nologin.php";
header("Location: $redirPage");
?>
Cheers,
Scorphus.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

thanks alot for the information. I just started learning this after 6 months of asp.....So i decided to switch to php. Is thier any good money in web design after you get good and all? Cause i would like to eventually become a software programmer. Once i go to college? Thanks again for all the help.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

?>
<?
if ($_POST["name"] == "gavin" and $_POST["pass"] == "socom")
$redirPage = "login.php";
else
$redirPage = "nologin.php";
header("Location: $redirPage");
?>


why is thier 2 equal signs? and the header does what for you? not really sure about that.?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Please note the if condition. See:

Code: Select all

<?
$_POST["name"] = "gavin"; // defining the variable $_POST["name"]

echo "\nContent of \$_POST["name"] is: " . $_POST["name"];

if ($_POST["name"] = "not_gavin") // using the assignment operator = return true in most cases
	echo "\nThis line will always be printed!";

echo "\nContent of \$_POST["name"] was changed to: " . $_POST["name"];

if ($_POST["name"] == "gavin") // using the assignment operator = return true in most cases
	echo "\nNow the if statement is working!";
else
	echo "\nAnd, in this case, this line will be printed, because we defined \$_POST["name"] in the first if condition!";
?>
this script outputs:

Code: Select all

Content of $_POST&#1111;"name"] is: gavin
This line will always be printed!
Content of $_POST&#1111;"name"] was changed to: not_gavin
And, in this case, this line will be printed, because we defined $_POST&#1111;"name"] in the first if condition!
I suggest you read these sections of the PHP Manual: We can see your logic programming skills are good, you're lacking some PHP basics. Also glad you moved from ASP, I'm sure it will be very pleasant to you.

Cheers,
Scorphus.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\russian\My Documents\bNi\login.php:17) in C:\Documents and Settings\russian\My Documents\bNi\login.php on line 115

^^^^^^^^^^^^that is what i get when i put this code between a table?

<?
if ($_POST['name'] == "gavin" and $_POST['pass'] == "socom")
header('Location: login.php');
else
header('Location: wrong.php');
exit()
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

ASP and PHP has somewhat similar syntax, but to help you in switching and understanding, try to write you code like following:

Code: Select all

if ($variable == TRUE) {
    echo "it was true";
    if ($foo = "bar") {
        echo "Foo was bar";
    }
} else {
    echo "it was not true";
}
Note the brackets and indendts. People write code in various ways, but yet I havn't seen anyone that I consider a good programmer skip the brackets. Just a tip.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

what is $foo ??? i have seen it like crazysince i started doing php 3 days ago?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

$foo is just to demonstrate a variable, and the word foo itself is just a common word in most programming languages.
webopedia.com wrote: Foobar is a universal variable understood to represent whatever is being discussed. It's usually used in examples that illustrate concepts and ideas in computer science.

For instance, a computer science professor may be discussing different file formats. In this case, he would call the generic-example file foo or foobar, then list the extensions associated with the file formats (e.g. foobar.txt, foobar.gif, foobar.exe, foobar.tar).

When foo or foobar is used, everyone understands that these are just examples, and they don't really exist.

Programmers and administrators also use foo and foobar in a similar context. Files or programs named with foo or foobar are understood not to be permanent and will be changed or deleted at anytime.

Foo, bar, and the compound foobar were commonly used at MIT, Stanford and the Helsinki University of Technology, Finland. Other generic variables are used other places, but only these three are considered universal.
gavinbsocom
Forum Commoner
Posts: 71
Joined: Tue Sep 30, 2003 9:51 pm

Post by gavinbsocom »

oh interesting, thanks
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

.Hm, perhaps I've got a sick mind or so, but did anybody have the bright idea to start a pub named "$foobar" yet ?
:D
Post Reply