php forwarding linkage from foreach to result - php

<?php
if(isset($_POST['MarkaSinifi'])) {
$MarkaSinifi = $_POST['MarkaSinifi'];
echo "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx";
foreach($MarkaSinifi as $marka) {
echo '&configoption[' . $marka . ']=1';
}
}
?>
I made that code for redirect user every $marka has a value like 1,2,3 this is the result for the link i want to redirect: "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption[1]=2&customfield[10]=webx&configoption[51]=1&configoption[52]=1"
This is just text, but i want to redirect users to that link. How can i do that?
This is what i did: https://coinearn.shop/marka.php

If you know all index of $marka, you can use
<?php
header('Location: '. "https://www.xxxxx.com/cart.php?a=add&pid=3&configoption$marka[0]=2&customfield[10]=webx&configoption$marka[1]=1&configoption$marka[2]=1");
This can help too
<?php
$args[0] = 'Location: https://www.xxxxx.com/cart.php?a=add&pid=3&configoption%s=2&customfield[10]=webx&configoption%s=1&configoption%s=1';
$args = array_merge($args, $marka);
header(call_user_func_array('sprintf', $args);

Related

Add more data into cookie in PHP

I have a cookie and I'm storing multiple checked checkbox values of page1 in this. Now I want to add more checked checkbox values into the cookie of page2 and then page3 and so on (I'm using pagination in my code). How can I do that in PHP?
<?php
$cookiename = "emp";
if (isset($_GET['check_list']) ) {
setcookie($cookiename , implode(',' , $_GET['check_list']) ,
time()+(86400/86400*30) );
}
?>
//to print the values
<?php
if (isset($_COOKIE[$cookiename]) ) {
print_r($_COOKIE);
}
?>
Maybe in this way:
<?php
$page = $_GET['page'];
$cookiename = "emp";
// GET COOKIE TO NOT OVERWRITE.
$cookie = isset($_COOKIE[$cookiename]) ? unserialize($_COOKIE[$cookiename]) : [];
if (isset($_GET['check_list']) ) {
$cookie[$page] = $_GET['check_list'];
setcookie($cookiename , serialize($cookie), time()+(86400/86400*30) );
}
?>
//to print the values
<?php
if (isset($_COOKIE[$cookiename]) ) {
print_r(unserialize($_COOKIE[$cookiename]));
}
?>
With unserialize you can store a multiple dimension array into a cookie with no problem.

$_GET to store a dynamic session

I'm a PHP noob and I need some help if you can.
I have a little website similar to a survey in witch i click on a link and in the next page I have get the previous variable and store it through all the survey.
In the first page I have 5 buttons to choose:
in the second page I use $_GET to have the first variable
<?php
// Starting session
session_start();
if (isset($_GET['q1'])){
$question_1 = $_GET['q1'];
$_SESSION['q1'] = $question_1;
echo '<br>';
echo $_SESSION['q1'];
}
else{
header('location: index.php');
exit();
}
?>
My problem is that is someone change the url (question-2.php?q1=ANYTHING-ELSE)
I need to redirect them to the home page.
Basically I need to have an If statement in witch I only echo the five results of the 5 href
So I can have one session $_SESSION['q1'] with dynamic content but restricted to this five.
I know it's wrong but i need to restrict the get to this:
if (isset($_GET['q1']) && (($_GET['q1'])= '1' or '2' or '3' or '4' or '5'){}
Thank you!
What you can do here is have an array of allowed values and check if q1 value is one of the values in the given array:
<?php
// Starting session
session_start();
$allowed = array(1, 2, 3, 4, 5);
if (isset($_GET['q1'])) {
$question = (int) $_GET['q1'];
if(in_array($question, $allowed, true)){
$_SESSION['q1'] = $question;
echo '<br>';
echo $_SESSION['q1'];
} else {
header('Location: index.php');
exit();
}
} else {
header('Location: index.php');
exit();
}

How to get a single variable attached to the URI?

I don't seem to able to acquire a GET variable that is attached to the URI.
The codes are => at the controller
...
parse_str($_SERVER['QUERY_STRING'], $_GET);
....
$data['ti'] = $this->input->get('hamleno');
this->load->view('anasayfa', $data);
The codes are => at the view the link is
...
<div class="row"><?php for ($b=0; $b<=(count($hml)-1); $b++) { ?><?php echo $hml[$b]." "; ?> <?php } ?></div>
The link is working. I have added the
$config['uri_protocol'] = "PATH_INFO";
to the config.php file.
However I am not able to get the $ti variable
if ($ti){
$t=$ti;
}else{
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) {
$t=$t+1;
if($t>($uz-1)){
$t=$uz-1;
}
} // Forward button was pressed;
if( $this->input->post('geri') ) {
$t=$t-1;
if($t<0){
$t=0;
}
} // Back button was pressed;
}
I'm not familiar with codeigniter, but I've always passed GET variables in this manner:
URL = www.site.com/folder/webpage.php?myvariable=myvalue
I would retrieve that value this way:
$x = $_GET['myvariable'];
or with codeigniter: (I think)
$x = $this->input->get('myvariable');
Tailored to your example, I would personally de-obfuscate your loop code just a little and instead of switching from PHP to HTML and back in one line, I would simply echo both from PHP like this:
(I also don't exactly understand the url you're using, so here is my approximate)
<?php
for ($b=0; $b<=(count($hml)-1); $b++)
{
echo '',$hml[$b],' ';
}
?>
I found out how Codeigniter solves the problem =>
$get = $this->uri->uri_to_assoc();
if(isset($get['hamleno'])){
$data['ti'] = $get['hamleno'];
}
This sends the $b assigned to $hamleno together with all the other stuff in $data.
Thank you all for your kind comments

PHP Multi URL Redirector with Array

Im trying to create a php to redirect multiple urls with an specific ID for example
mysite.com/url.php?id=1
id=1 will automatically redirect to www.google.com
mysite.com/url.php?id=2
id = 2 will automatically redirect to www.bing.com
successively ...
I think that an array can simplify it
i will apreciate your answers.
Thanks
If you want to use a static array, then use the following code:
<?php
$sites = array(
1 => 'http://domain.tld';
2 => 'http://anotherdomain.tld';
);
if (isset($_GET['id']) && array_key_exists($_GET['id']), $sites) {
header('location: ' . $sites[$_GET['id']]);
} else {
// 404?
}
?>
You can use something like this:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
$id = (int)(trim($id));
switch($id){
case 1:
header("Location: http://www.google.com/");exit;
break;
case 2:
header("Location: http://www.bing.com/");exit;
break;
default:
header("Location: http://www.default.com/");exit;
break;
}
?>

PHP include pages error

Still trying to get how PHP works :)
Kindly help me with one solution & idea, here is when i'm using:
<?php
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if(in_array($_GET['show'], array_keys($content))) {
include($content[$_GET['show']]);
} else {
include('sub_id00.php');
}
?>
and:
<?php
$content = array(
'id00'=>'N/A',
'id01'=>'ID01',
'id02'=>'ID02',
);
if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
echo $content[$_GET['show']];
} else {
echo $content['id00'];
}
?>
Where first example includes pages and second includes simple code inside ''
Problem is that if there is no ID set (index.php) ut shows default page/code. And if wrong ID set, it'll also show default page/code.
How to update it so without any ID set it would show default page and if some wrong ID set, it would show some error page/code?
Cheers!
UPDATE!
After a while reqding i've updated it with:
<?php
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if (in_array($_GET['show'], array_keys($content)))
{
include($content[$_GET['show']]);
}
elseif (isset($_GET['show']))
{
include('sub_error.php');
}
else {
include('sub_id00.php');
}
?>
And:
<?php
$content = array(
'error'=>'error msg',
'id00'=>'N/A',
'id01'=>'ID01',
'id02'=>'ID02',
);
if(!empty($_GET['show']) && isset($content[$_GET['show']]))
{
echo $content[$_GET['show']];
}
elseif (isset($_GET['show']))
{
echo $content['error'];
}
else
{
echo $content['id00'];
}
?>
:)
$content = array(
'id01'=>'sub_id01.php',
'id02'=>'sub_id02.php'
);
if (isset($_GET['show']))
{
if (array_key_exists($_GET['show'], $content))
{
//$_GET id is set and it exists in content
include($content[$_GET['show']]);
}
else
{
//$_GET id is set but does not exist in content
//include whatever page you have for a wrong id here
}
}
else
{
//else no $_GET was set
//include default page
}

Categories