Using URL With Variables to Effect Code - php

I was wondering how you use ? (question marks) in your website to make it do different stuff based on the variable in the url.
like:
http://example.com/php/learn/kinder.php?level=$level
like what's that called, and how do you use it?
I assume with a switch statement
This is my code at the momment:
<php
$con=mysqli_connect("host","username","pass","db");
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$level = mysqli_query($con,"SELECT gradek FROM profiles");
?>
<html>
<head>
<title> Redirecting </title>
<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=$level>
</head>
<body>
</body>
</html>
Then i'd have a switch... I know it's pointless. I just want to know for future reference.
So my question is:
how to redirect using php variable in html
How to use the ? to change the code.
I used bolli's code and this is what appears in the web page:
`<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level2=> </head> <body> </body> </html>
And it still doesn't redirect correctly

In kinder.php place:
$level = $_REQUEST['level'];
echo $level;
And read about POST and GET request.
http://www.tizag.com/phpT/postget.php
Or do you mean how to place the variable in the URL?
Either do
<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=<?php echo $level;?>>
or
echo "<meta http-equiv="refresh" content="1; url=http://teachertechtutor.com/php/learn/kinder.php?level=".$level;
EDIT / UPDATE
Are you trying to get the variable and based on it, switch the page content?
You could do something like this?
<?php
//you could load header here
//include 'header.php';
/*
*Load content based on $_get parameter
*/
// Get the $variable from the url after the level=
$page = (isset($_GET['level'])) ? $_GET['level'] : 'index';
//Check to see if the file exist
if (!file_exists($page . '.php'))
{
//echo "File does not exist" . $page;
}
switch ($page)
{
case 'test':
include('test.php');
break;
case 'test2':
include('test2.php');
break;
case 'test3':
include('test3.php');
break;
//Defualt start page
default:
include('index.php');
}
// You could load footer here
//include 'footer.php';
?>

Related

meta redirect with php inside

I have a php file. In it I want an error redirect like this:
<?php
$ponka = somerandomsite.com;
if (1=1)
{echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://$ponka/error.php">'}
?>
How can I achieve it?
You php contains lots of errors, here's a working sample
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=http://$ponka/error.php'>";
}
?>
Also, remeber that php as a function called header
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
header("Location: http://$ponka/error.php");
}
?>

How can I use variables defined later in the page? PHP

This is how I have my files setup:
INDEX.PHP:
include($sys->root_path.'sections/start.php'); // Includes everything from <html> to where we are now
if (isset($_GET['page'])) {
$page = $_GET['page'].'.php';
} else {
$page = 'index.php';
}
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = '';
}
include($sys->root_path.'/content/'.$cat.'/'.$page);
include($sys->root_path.'sections/end.php'); // Includes everything from here to </html>
To view this page, I visit: example.com/index.php?cat=red&page=car
This will show me a page with the content of the file at:
/content/red/car.php
The problem I am having is that I want to specify a title, meta description, etc. for each page, and this is outputted to the page in start.php- before any specific data for this particular page is called.
What I was hoping to do is something like this:
/CONTENT/RED/CAR.PHP:
<?php $page_title = 'Title of the page'; ?>
<p>Everything below is just the page's content...</p>
How can I use this page specific data in the <head> of the site, when all that data is grabbed before the contents of this specific page?
You could do something like:
switch($_GET['page']) {
case 'car':
// Here you could have another conditional for category.
$page_title = 'Title of the page';
break;
// Other cases.
}
include($sys->root_path.'sections/start.php');
And in start.php you could have something like:
<title><?php echo $page_title; ?></title>
I must advise against that way of including content. It is insecure. Someone could browse your server files or include something you don't want included. One should never include files that way (through get variables) unless one always filter that variable through a regular expression or something else.
The correct way to do what you're trying to do is with a database and
apache url_rewrite. My answer is just a fix for your problem.
Step 1
Include start.php below the if statment, this way when you include start.php you already know which page you need, like this:
if (isset($_GET['page'])) {
$page = $_GET['page'].'.php';
} else {
$page = 'index.php';
}
if (isset($_GET['cat'])) {
$cat = $_GET['cat'];
} else {
$cat = '';
}
include($sys->root_path.'sections/start.php');
Step 2
Now, inside the start.php use a switch:
<?php
switch ($cat) {
case "blue":
$title = "The car is $page";
break;
case "green":
$title = "The car is $page";
break;
case "red":
$title = "The car is $page";
break;
default:
$title = "Welcome to ...";
}
?>
<!DOCTYPE html>
<head>
<title><?php echo $title ?></title>
</head>
etc...
My recommendation would be to use an MVC approach with either a function to pass the parameters or OOP with a setter function.

default case in a PHP switch-case statement doesn't really work as the default?

I'm using the following PHP code, on a page that a user reach on after submitting a form with a certain foo input name in it, which this PHP code processes, and decides to which URL that user be forwarded accordingly.
I've just noticed that If a user does not enter that page with an foo input name (for example, let's say the form on the previous page had a `vvv' as the input name instead, due to some error),
then this PHP code would not send the user to the default URL. instead, it would refresh itself every 3 seconds in a loop.
Why? shouldn't the default value be obtained in case of any error, including the above scenario?
CODE:
<?php
if(isset($_POST['foo'])){
switch ($_POST['foo']) {
case "aaa":
$url = "http://www.aaa.com/";
break;
default:
$url = "http://www.bbb.com/";
}
}
header( "refresh:3;url=$url" );
?>
<!doctype html>
<html>
<head>
<style>
.test {display: block;}
</style>
</head>
<body>
test
</body>
</html>
1) I think you are messing with $_POST and $_GET. Get params are visible in URL, POST is not shown in url.
2) If there is no $_POST['foo'], than it will throw error, because there is no set $url variable. Better change to this code:
if(isset($_GET['foo'])){
switch ($_GET['foo']) {
case "aaa":
$url = "http://www.aaa.com/?foo=bbb";
header( "refresh:3;url={$url}" );
break;
default:
$url = "http://www.bbb.com/?foo=aaa";
header( "refresh:3;url={$url}" );
}
die(); // Optionally, if you wish not to continue any script
} else {
/* Show default code */
}
Please verify that the switch snipet is reached when this page is navigated to without foo parameters.
In your case, the $url has not been initiated yet (may be the if condition was failed). So that the header function will actually send the browser this header: refresh:3;url= (empty string after url=), which cause the browser refresh with current url every 3 seconds.
<?php
$url = "http://www.bbb.com/";
if(isset($_POST['foo'])){
switch ($_POST['foo']) {
case "aaa":
$url = "http://www.aaa.com/";
break;
}
}
header( "refresh:3;url=$url" );
?>
<!doctype html>
<html>
<head>
<style>
.test {display: block;}
</style>
</head>
<body>
test
</body>
</html>
It would be better if you set the default url at the top. So even if none of the case get match. Default value will always be there. If the value in case gets matched, then it will be replaced.
Does $_POST['foo'] has any value at all?
Try dump the value to check if there is anything in it:
var_dump($_POST['foo'])
If the result of this is null then there is an error in your form.
Something like this should work:
<form method='post' action = $this->url()>
<div>
<input type='text' name='foo' value='foo'>
</div>
<div>
<input type='submit' value='submit' name='submit'>
</div>
</form>
And the action:
if(isset($_POST['foo']) && !empty($_POST['foo'])){
switch ($_POST['foo']) {
case "aaa":
$url = "http://www.aaa.com/";
break;
default:
$url = "http://www.bbb.com/";
}
}

PHP subdomain redirect according to language variable

I'm trying to write a php script that checks the language(which is defined by the language function in $language) for a value and if user requests any address, www.example.com/foo/bar/data.php?=foobar it will redirect him by http refresh or redirect or header location(not preferable) to subdomain.example.com/$1 ($1 as in the same original requested address).
something like this but without the header location:
<?php if ($language == "en") { header ("Location: http://"$language".example.com/"$1""); } ?>
this does not work, also I get an error in the log "header already sent by another file" which is another script I got running and cannot change the code.
So, what I need is a script that reads the variable and according to its value it will redirect the user to the appropriate subdomain.
Hi you can echo a javascript code that contains redirection. Try this one.
<?php
if($language === "en"){
echo "<script type='text/javascript'> document.location = 'http://' . $language . '.example.com/' . $1; </script>";
}
?>
This code works most for me compared to header('Location: etc...').
<?php
// Language detection code
// ...
if ('en' === $language)
{
header('Location: http://' . $language . '.example.com' . $_SERVER['REQUEST_URI']);
exit();
}
If you really can't change the code from the other file, you'll need to make an html redirection instead.
<?php
// Language detection code
// ...
if ('en' === $language)
{
$url = 'http://' . $language . '.example.com' . $_SERVER['REQUEST_URI'];
echo <<<EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Refresh" content="0; url={$url}" />
</head>
<body></body>
</html>
EOF;
exit();
}

Passing $_SESSION through an iframe - PHP

For the record, I DID see this entry - Passing sessions variables through an iframe, php which is asking the exact same question, but the answer (even when I followed it to a T) still isn't working for me.
I have two pages - one is the landing page, the other is the page pulled in to the landing page via iframe. I start the session on the landing page and assigned $_SESSION a value, and I want that value pulled in to the iframe.
Here's my code for the landing page:
<?php
session_start();
$_SESSION['vendorname'] = $this->getVendorId(); // store session vendor name data
echo "Vendor = ". $_SESSION['vendorname']; // test to see if the vendor name was properly set
session_write_close();
?>
<html>
<body>
....blah blah...
<iframe width="100%" src="http://www.somewhere.com/iframe.php"></iframe>
<body>
</html>
Here's the code from the page within the iframe:
<?php
session_start();
?>
<html>
<head>
<link href="css/something.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
if(isset($_SESSION['vendorname']) && is_array($_SESSION['vendorname'])) {
echo "vendor = ". $_SESSION['vendorname']; }
else {
echo "Meh, back to the drawing board"; }?>
</body>
</html>
On the landing page, the $_SESSION displays correctly. It retrieves the vendor's name via our database and spits it out on the screen. In the iframe however, it only displays my failure message ("Meh, back to the drawing board"). I am missing something. :(
EDIT:
Per Marc B's suggestion, I'm now checking the session_id(). So for this code (on landing page):
<?php
session_start();
echo session_id();
echo "<br>";
$_SESSION['vendorname'] = $this->getVendorId(); // store session vendor name data
echo "Vendor = ". $_SESSION['vendorname']; //test to see if the vendorname was properly set
echo "<br>";
echo session_id();
session_write_close();
?>
I'm getting the following output:
0lq5gb79p52plgd9mcknpife60
Vendor = SUPERVEND
0lq5gb79p52plgd9mcknpife60
On the iframe page, for this code:
<?php
session_start();
echo session_id();
?>
<html>
<head>
<link href="css/something.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
echo session_id();
if(isset($_SESSION['vendorname'])) {
echo "vendor = ". $_SESSION['vendorname']; }
else {
echo "vendor = ". $_SESSION['vendorname']; }
?>
I'm getting the following output:
0lq5gb79p52plgd9mcknpife60
0lq5gb79p52plgd9mcknpife60
vendor =
Start the iframe with the following:
header('P3P: CP="CAO PSA OUR"');
session_start();
you should then be able to access the session variables in the normal fashion.

Categories