Page 1 of 1

redirecting pages

Posted: Tue Sep 30, 2003 9:51 pm
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?

Posted: Tue Sep 30, 2003 10:19 pm
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.

Posted: Wed Oct 01, 2003 9:06 am
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...

Posted: Wed Oct 01, 2003 9:20 am
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

Posted: Wed Oct 01, 2003 9:26 am
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.

Posted: Wed Oct 01, 2003 9:47 am
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.

Posted: Wed Oct 01, 2003 9:49 am
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.?

Posted: Wed Oct 01, 2003 9:53 am
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.

Posted: Wed Oct 01, 2003 5:04 pm
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()
?>

Posted: Wed Oct 01, 2003 5:13 pm
by volka

Posted: Wed Oct 01, 2003 5:52 pm
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.

Posted: Wed Oct 01, 2003 8:01 pm
by gavinbsocom
what is $foo ??? i have seen it like crazysince i started doing php 3 days ago?

Posted: Wed Oct 01, 2003 8:12 pm
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.

Posted: Wed Oct 01, 2003 9:23 pm
by gavinbsocom
oh interesting, thanks

Posted: Thu Oct 02, 2003 4:10 am
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