How i can put string in a session?
for. e.g. : $_SESSION_[$questioncounter+'question'] = $accepted;
if _$questioncounter = 2_, this mean $_SESSION_['2question']
Use a .(dot) to concat string and variable and remove _ from $_SESSION_ try
$_SESSION[$questioncounter.'question'] = $accepted;
so full code :-
<?php
session_start();
$questioncounter = 2;
$accepted = 'yes';
$_SESSION[$questioncounter.'question'] = $accepted;
echo $_SESSION['2question']; // yes
?>
Well, sometimes concatenation is needed but not in this case. Building variables by concatenating strings in this case would be the worst approach.
This is the way you should do things
// start session
if(!isset($_SESSION)){
session_start();
}
// now, add your questions to the $_SESSION this way
$question = array('Is the sky really blue?', 'Should I stay or should I go?', 'Why I can\'t fly');
$_SESSION['question'] = $question;
// or this way
$_SESSION['question'][0] = 'Is the sky really blue?';
$_SESSION['question'][1] = 'Should I stay or should I go?';
...
// add their status this way
$_SESSION['question'][0]['accepted'] = 1; // or $_SESSION['question'][0]['status'] = 1;
$_SESSION['question'][1]['accepted'] = 0;
// and finally use them like this
echo $_SESSION['question'][0];
// or
if($_SESSION['question'][0]['accepted']){
// say 'Bravo!'
}
Related
i was doing validation for user form after each validation i used to store "valid" in a array index and at last comparing them like this:
if(isset($fullname)){
if ($valid["name"]=="valid"&&$valid["username"]=="valid"&&$valid["password"]=="valid"&&$valid["email"]=="valid") {
session_start();
$_SESSION["reg_name"] = $fullname1;
$_SESSION["reg_username"] = $username1;
$_SESSION["reg_email"] = $email1;
$_SESSION["reg_password"] = $password1;
$_SESSION["reg_gender"] = $_REQUEST['gender'];
header("location:validation&insertion.php");
}
Well i will check validation and then make session .
My question is there any short way to check the whole array across a single value like "valid"?
I hope you have understand my question.Comment it if it is not asked well.
Do not rate as negative.Please ignore my grammar mistakes.I hate those who edit my question's grammar.
You can just count the number of unique values and check if it's equal to 1, then check one value if it is "valid".
if (count(array_unique($valid)) === 1 && $valid["name"] === "valid") {
session_start();
$_SESSION["reg_name"] = $fullname1;
$_SESSION["reg_username"] = $username1;
$_SESSION["reg_email"] = $email1;
$_SESSION["reg_password"] = $password1;
$_SESSION["reg_gender"] = $_REQUEST['gender'];
header("location:validation&insertion.php");
}
Or just simply check if a "notvalid" value is found in the array:
if (!in_array("notvalid", $valid)) {
session_start();
$_SESSION["reg_name"] = $fullname1;
$_SESSION["reg_username"] = $username1;
$_SESSION["reg_email"] = $email1;
$_SESSION["reg_password"] = $password1;
$_SESSION["reg_gender"] = $_REQUEST['gender'];
header("location:validation&insertion.php");
}
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
I am writing code for math problems -- they follow a format, with randomly generated variables, and then I lock in the variables per the code below. What isn't working -- form03 allows the user to finish the page of math problems and to reset for another. I need to destroy the session on that condition. But even when I enter data in form03, so that it isset, the old session values remain.
???
require_once 'random.php';
require_once 'forms-functions.php';
if (isset($_SESSION['z'])) {
$_SESSION['y'] = "";
session_start();
session_destroy();
}
session_start();
if (isset($_SESSION['y'])) {
echo "hello isset<br>";
$x01 = $_SESSION['x01'];
$x02 = $_SESSION['x02'];
$output01b = $_SESSION['output01b'];
$output02b = $_SESSION['output02b'];
} else {
echo "hello else<br>";
ob_start();
random1();
$output01 = ob_get_clean();
$output01b = "single string: ".$output01."";
$x01 = $x;
ob_start();
random1();
$output02 = ob_get_clean();
$output02b = "single string: ".$output02."";
$x02 = $x;
$_SESSION['x01'] = $x01;
$_SESSION['x02'] = $x02;
$_SESSION['output01b'] = $output01b;
$_SESSION['output02b'] = $output02b;
$y = "1";
$_SESSION['y'] = $y;
}
echo $output01b;
$user_input01 = form01('user_input01');
echo $output02b;
$user_input02 = form02('user_input02');
$user_input03 = form03('user_input03');
if(isset($user_input03)) {
$z = 1;
$_SESSION['z'] = $z;
echo "hello \$z";
}
You need to call session_start() before you try to access the session variable z. And you need to set the new session variable y after you destroy the old session and start a new one.
session_start();
if (isset($_SESSION['z'])) {
session_destroy();
session_start();
$_SESSION['y'] = "";
}
Basically I'm grabbing a variable from the url and I need to send the user to custom pages based on what state they fill in. i.e. if it's one of the coverred states, they'll get sent to custom pages...but if it's any other state they get sent to the standard one.
I'm guessing it's just a simple if statement with an else...but for some reason I can't get it to work properly.
<?php
$state = urlencode($_GET['state']);
if ($state=="california"){$link = "http://website.com/page1";}
else if ($state=="new york"){$link = "http://website.com/page2";}
else if ($state=="ohio"){$link = "http://website.com/page3";}
else {$link = "http://website.com/default";}
header("Location: $link");
?>
Is this right or should I be doing something else?
You don't need urlencode function to encode what you "GET", unless you sent the string in the url has been encoded. However, in that case you would need the urldecode function, still not urlencode.
So your code should be like this:
<?php
$state = $_GET['state'];
//or $state = urldecode($_GET['state']);
//if you are receving an encoded url.
if ($state=="california"){$link = "http://website.com/page1";}
else if ($state=="new york"){$link = "http://website.com/page2";}
else if ($state=="ohio"){$link = "http://website.com/page3";}
else {$link = "http://website.com/default";}
header("Location: $link");
?>
Also, check the 'state' in the url. Did you receive the correct string that you need? Try to echo $state, and see what you get.
The urlencode is not needed and even introduces a bug (you will never match strings such as "new york", because urlencode will turn $state into "new+york").
Other than that the code looks OK. Just remove it and you 'll be good.
No need to use urlencode
<?php
$state = $_GET['state'];
if ($state=="california"){
$link = "http://website.com/page1";
} else if ($state=="new york") {
$link = "http://website.com/page2";
} else if ($state=="ohio"){
$link = "http://website.com/page3";
} else {
$link = "http://website.com/default";}
header("Location: $link");
?>
It is tidier to do this with switch, and combine the repeated stuff:
switch($_GET['state']) {
case "california": $page = "page1"; break;
case "new york": $page = "page2"; break;
case "ohio": $page = "page3"; break;
default: $page = "default"; break;
}
header("Location: http://website.com/".$page);
Another option is to use an associative array; this is attractive if there are lots of choices.
$statemap = array('california' => 'page1',
'new york' => 'page2',
'ohio' => 'page3');
$state = $_GET['state'];
$link = 'http://website.com/' . array_key_exists($state, $statemap) ? $statemap[$state] : 'default'
header("Location: $link");
I am writing what I thought would be a simple script but I am stuck.
The scenario is that I want to create 2 strings from the GET request.
eg: domain.com/script.php?Client=A12345
In script.php it needs to grab the "Client" and create 2 variables. One is $brand and needs to grab the A or B from the URL. The Other is $id which needs to grab the 12345 from the URL.
Now, after it has these 2 variables $brand and $id it needs to have an if statement to redirect based on the brand like below
if ($brand=="A") {
header('Location: http://a.com');
}
if ($brand=="B") {
header('Location: http://b.com');
At the end of each URL I want to apend the $id though and I am unsure on how to do this.
So for example I would access the script at domain.com/script?Client=A1234 and it needs to redirect me to a.com/12345
Thanks in advance!
$fullCode = $_REQUEST['Client'];
if(strpos($fullCode, 'A') !== false) {
$exp = explode('A',$fullcode);
header('Location: http://a.com/' . $exp[1]);
}
else if(strpos($fullCode, 'B') !== false) {
$exp = explode('B',$fullcode);
header('Location: http://b.com/' . $exp[1]);
}
else {
die('No letter occurence');
}
You can easily do,
$value = $_GET['Client'];
$brand = substr($value, 0, 1);
$rest = substr($value, 1, strlen($brand)-1);
now you have the first character in $brand string and you can do the if statement and redirect the way you want...
You mean like this?
Notice: this will only work if brand is just 1 character long. If that's not the case, please give better examples.
<?php
$client = $_GET['Client'];
$brand = strtolower(substr($client, 0, 1));
$id = substr($client, 1);
if ($brand == 'a')
{
header("Location: http://a.com/$id");
}
elseif ($brand == 'b')
{
header("Location: http://b.com/$id");
}
?>
Try using:
preg_match("/([A-Z])(\d*)/",$_GET['Client'],$matches);
$matches[1] will contain the letter and $matches[2] will contain your id.
Then you can use:
if ($matches[1]=="A")
{
header('Location: http://a.com/{$matches[2]}');
}
if ($matches[1]=="B")
{
header('Location: http://b.com/{$matches[2]}');
}
suggest you could also try
$requested = $_GET["Client"];
$domain = trim(preg_replace('/[^a-zA-Z]/',' ', $requested)); // replace non-alphabets with space
$brand = trim(preg_replace('/[a-zA-Z]/',' ', $requested)); // replace non-numerics with space
$redirect_url = 'http://' . $domain . '/' . $brand;
header('Location:' . $redirect_url);
but it'd be better if you could get the domain name and brand as two individual parameters and sanitize them individually before redirecting them to prevent the overhead of extracting them from a single parameter.
Note: this expression might be useless when the domain name itself has numerics and because the Client is obtained through get a good deal of validation and sanitation would be required in reality.
$brand = strtolower($_GET['Client'][0]);
$id = substr($_GET['Client'], 1);
header("Location: http://{$brand}.com/{$id}");
If for some purpose you want to use explode, then you need to have a separator.
Let's take '_' as the separator, so your example would be something like this: domain.com/script.php?Client=A_12345
$yourstring = explode("_",$_GET["Client"]);
echo $yourstring[0];
//will output A
echo $yourstring[1];
//will output 12345
//your simple controller could be something like this
switch($yourstring[0]){
case: 'A':
header('Location: http://a.com?id='.$yourstring[1]);
exit();
break;
case: 'B':
header('Location: http://b.com?id='.$yourstring[1]);
exit();
break;
default:
//etc
}