redirecting pages
Moderator: General Moderators
-
gavinbsocom
- Forum Commoner
- Posts: 71
- Joined: Tue Sep 30, 2003 9:51 pm
redirecting pages
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?
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
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:
Regards,
Scorphus.
Here is a simple code:
Code: Select all
<?
$redirect = 'Ok';
if ($redirect == 'Ok')
header('Location: page1.php');
else
header('Location: page2.php');
?>Scorphus.
-
gavinbsocom
- Forum Commoner
- Posts: 71
- Joined: Tue Sep 30, 2003 9:51 pm
In scorphus example $redirect was a variable.
The code you have written, re-written correctly in PHP would look like this
Glad you saw the light and swithched to PHP
Mark
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.
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
No! Everything after a $ in PHP is a variable. So this:is the same as (ASP):
I would say:
or:
Cheers,
Scorphus.
Code: Select all
<?
$pi = 3.14159265359;
?>Code: Select all
<%
Dim pi
pi = 3.14159265359
%>Code: Select all
<?
if ($_POST["name"] == "gavin" and $_POST["pass"] == "socom")
header("Location: login.php");
else
header("Location: nologin.php");
?>Code: Select all
<?
if ($_POST["name"] == "gavin" and $_POST["pass"] == "socom")
$redirPage = "login.php";
else
$redirPage = "nologin.php";
header("Location: $redirPage");
?>Scorphus.
-
gavinbsocom
- Forum Commoner
- Posts: 71
- Joined: Tue Sep 30, 2003 9:51 pm
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
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
Please note the if condition. See:
this script outputs:
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.
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!";
?>Code: Select all
Content of $_POSTї"name"] is: gavin
This line will always be printed!
Content of $_POSTї"name"] was changed to: not_gavin
And, in this case, this line will be printed, because we defined $_POSTї"name"] in the first if condition!Cheers,
Scorphus.
-
gavinbsocom
- Forum Commoner
- Posts: 71
- Joined: Tue Sep 30, 2003 9:51 pm
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()
?>
^^^^^^^^^^^^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()
?>
please read viewtopic.php?t=1157
ASP and PHP has somewhat similar syntax, but to help you in switching and understanding, try to write you code like following:
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.
Code: Select all
if ($variable == TRUE) {
echo "it was true";
if ($foo = "bar") {
echo "Foo was bar";
}
} else {
echo "it was not true";
}-
gavinbsocom
- Forum Commoner
- Posts: 71
- Joined: Tue Sep 30, 2003 9:51 pm
$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.
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany