i'm very newbie trying to finish a job, so let me explain the issue here:
i have a chasback website, there are retailers on database, my links are :
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
The php page that show the content have:
if (isset($_GET['rid']) && is_numeric($_GET['rid']))
{
$retailer_id = (int)$_GET['rid'];
}
else
{
header ("Location: index.php");
exit();
}
That display on my URL: /view_retailer?rid=8
I want to change that to /view_retailer?rid=retailer-title
I can't figure why its not working, i have on my DB the columns retailer_id and title.
Then i need to change from /view_retailer?rid=retailer-title to /loja/retailer-title through mod_rewrite.
How can i reach there? thanks for helping me!
I can answer the first bit
a href="/view_retailer.php?rid=<?php echo $tops_row['retailer_id']; ?>"
Needs to be:
<a href="/view_retailer.php?rtitle=<?php echo $tops_row['retailer_title']; ?>"
And
if (isset($_GET['rtitle']) && is_string($_GET['rtitle']))
{
$retailer_title = (string)$_GET['rtitle'];
}
else
{
header ("Location: index.php");
exit();
}
You should also sanitise anything coming from GET/POST to make sure it is safe if you intent to query the database with it.
As for the mod_rewrite. In very rubbish at that so I would not be able to help with that.
How about using 3 tables in your DB?
retailer_id retailer_name and retailer_url_name
values (example)
43 The Awesome Supermarket and awesome-supermarket
Check $_GET['rid'] with a regex pattern to check for letters and the - character and match against retailer_url_name in the DB?
// check for letters and -
if (preg_match('/^[a-z\-]+$/', $_GET['rid']) {
// do stuff here
} else {
// invalid ID
}
In the retailers table add column named "slug" or something like that. Then you have to modify you query thus you are not looking for id but certain string (slug). Make sure that your slugs contains only signs proper for web addresses: no spaces, non-latin letters etc.
When it comes to code, it may look something like that:
if (!empty($_GET['title']))
{
$title = someSanitizeFunc($_GET['title']);
$retailer = db()->queryOne("SELECT id FROM retailers where slug = '".$title."');
}
It's just example, you have to adjust it to your cms or whatever you use.
Related
While I refresh my browser the entries of the registration form goes into the Database every time i press REFRESH, Professor told me to resolve this problem with the help of LAST_INSERT_ID().
I am able to get the last_insert_id from the database but doesn't know what would I do further with that ID.
Please help..
enter image description here
The recommended way is to use the Post/Redirect/Get pattern.
There are other ways to achieve what you desire here.
I am not sure what your professor is asking to do with the last insert id. Maybe he is referring to something like this,
if(isset($_SESSION['last_insert_id'])){ // At the beggining
//redirect to a another location
}
// Code for insertion goes here
$_SESSION['last_insert_id'] = $last_insert_id; // Get and store the insertion id as a session
I think you are using the same page to Save the Data, If it is So, then follow the following method :
<?php
if(isset($_POST[userName]))
{
// Put your Registration Operation Code Here
header('Location: ./Registrationform.php');
}
?>
After DataBase Insertion it redirects to the same page. Now Refresh is made with the GET Method not by the POST Method. So, you can eliminate the duplicate entries by this way.
As per your requirement I used the Last Inserted ID for the Validation before Inserting Records in the Database.
<?php
session_start();
if(isset($_POST[userID]))
{
$flag = false;
if(isset($_SESSION['last_insert_id']))
{
if($_SESSION['last_insert_id'] == $_POST[userID])
{
$flag = false;
header('Location: ./Registrationform.php');
}
else
{
$flag = true;
$_SESSION['last_insert_id'] = $_POST[userID];
}
}
if($flag == true)
{
// Put your Registration Operation Code Here
}
}
?>
In my code i have an if statement, which is return is true, it would header the user to a specific page. But i want to send some data to the next page. So i tried using dynamic links. but it doesn't seem to work. Here's my code;
<?php
$row = mysqli_fetch_object($query);
if($row->Usertype = "General_User")
{
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid= echo $row->Company_ID");
}
else
{
header('Location: http://www.mywebsite.com');
}
?>
but when i'm redirected to the page, i get this;
http://www.mywebsite.com/GeneralUserHome.php?cid=%20echo%20'';
any suggestions?
Why do you have echo in there?
header("Location: http://www.mywebsite.com/GeneralUserHome.php?cid={$row->Company_ID}");
You have a typo here:
if($row->Usertype = "General_User")
That's an assignment, and will always be true. You want double-equals for comparison:
if($row->Usertype == "General_User")
As a note, I reverse the two in order to avoid these typos. This will error out and tell you exactly what was wrong, if you typo'd a single equals sign:
if("General_User" = $row->Usertype)
I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1
I am redirecting to a different page with Querystring, say
header('location:abc.php?var=1');
I am able to display a message on the redirected page with the help of querystring value by using the following code, say
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
echo 'Done';
}
}
But my problem is that the message keeps on displaying even on refreshing the page. Thus I want that the message should get removed on page refresh i.e. the value or the querystring should not exist in the url on refresh.
Thanks in advance.
You cannot "remove a query parameter on refresh". "Refresh" means the browser requests the same URL again, there's no specific event that is triggered on a refresh that would let you distinguish it from a regular page request.
Therefore, the only option to get rid of the query parameter is to redirect to a different URL after the message has been displayed. Say, using Javascript you redirect to a different page after 10 seconds or so. This significantly changes the user experience though and doesn't really solve the problem.
Option two is to save the message in a server-side session and display it once. E.g., something like:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
This can cause confusion with parallel requests though, but is mostly negligible.
Option three would be a combination of both: you save the message in the session with some unique token, then pass that token in the URL, then display the message once. E.g.:
if (isset($_GET['message'], $_SESSION['messages'][$_GET['message']])) {
echo $_SESSION['messages'][$_GET['message']];
unset($_SESSION['messages'][$_GET['message']]);
}
Better use a session instead
Assign the value to a session var
$_SESSION['whatever'] = 1;
On the next page, use it and later unset it
if(isset($_SESSION['whatever']) && $_SESSION['whatever'] == 1) {
//Do whatever you want to do here
unset($_SESSION['whatever']); //And at the end you can unset the var
}
This will be a safer alternative as it will save you from sanitizing the get value and also the value will be hidden from the users
There's an elegant JavaScript solution. If the browser supports history.replaceState (http://caniuse.com/#feat=history) you can simply call window.history.replaceState(Object, Title, URL) and replace the current entry in the browser history with a clean URL. The querystring will no longer be used on either refresh or back/previous buttons.
When the message prompt ask for a non exsisting session. If false, show the message, if true, do nothing. session_start(); is only needed, if there is no one startet before.
session_start();
if ($_GET['var']==1 && !isset($_SESSION['message_shown']))
{
$_SESSION['message_shown'] = 1;
echo 'Done';
}
Try this way [Using Sessions]
<?php
//abc.php
session_start();
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
if(isset($_SESSION['views']))
{
//$_SESSION['views']=1;
}
else
{
echo 'Done';
$_SESSION['views']=1;
}
}
}
?>
Think the question mean something like this?
$uri_req = trim($_SERVER['REQUEST_URI']);
if(!empty($_SERVER['REQUEST_URI'])){
$new_uri_req = str_replace('?avar=1', '?', $uri_req);
$new_uri_req = str_replace('&avar=1', '', $new_uri_req);
$pos = strpos($new_uri_req, '?&');
if ($pos !== false) {
$new_uri_req = str_replace('?&', '?', $new_uri_req);
}
}
if( strrchr($new_uri_req, "?") == '?' ){
$new_uri_req = substr($new_uri_req, 0, -1);
}
echo $new_uri_req; exit;
You can use then the url to redirect without vars. You can also do the same in js.
str_replace() can pass array of values to be replaced. First two calls to str_replace() can be unified, and filled with as many vars you like that needs to be removed. Also note that with preg_replace() you can use regexp that can so manage any passed var which value may change. Cheers!
I have a problem:
Here's a block of the code:
function draw()
{
$out_string="";
$out_string.=$this->script;
reset($this->fields);
$num_list_box=0;
while( $field = each($this->fields) )
{
if (isset($this->fields[$field[1]->field]->options))
{
if (preg_match("/<script type=\"text\/javascript\">/i",$this->fields[$field[1]->field]->options[0][1])&& $this->fields[$field[1]->field]->value!="")
{
if ($num_list_box==0) $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
else $out_string.= "<script type=\"text/javascript\">levels.forValue(\"".$field_prev[0]."\").forValue(\"".$field_prev[1]."\").setDefaultOptions(\"".$this->fields[$field[1]->field]->value."\");</script>\n";
$field_prev[]=$this->fields[$field[1]->field]->value;
$num_list_box++;
} else
{
$field_prev[0]=$this->fields[$field[1]->field]->value;
$num_list_box=0;
}
}
}
$out_string.=$this->draw_title();
$out_string.=$this->draw_header();
$out_string.= "<table class=\"forms\">\n";
$field=array_keys($this->fields);
reset($field);
$ind_first=true;
while( list($pos,$field_name) = each($field) )
{
if ($this->num_cols>0) {
if ($this->fields[$field_name]->col==1){
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_title\">";}
else $out_string.="<td class=\"field_title\">";
$out_string.= $this->fields[$field_name]->title."</td>";
$colspan="";
if ($this->num_cols>1) {
if ($this->fields[$field_name]->col==1 && array_key_exists($pos+1,$field) && $this->fields[$field[$pos+1]]->col==1)
$colspan="colspan=\"3\"";
}
$out_string.="<td class=\"field_value\" $colspan>";
$out_string.=$this->fields[$field_name]->draw()."</td>";
} else
{
if ($ind_first) $ind_first=false;else $out_string.="</tr>";
$out_string.="<tr><td class=\"field_value\">".$this->fields[$field_name]->title."<br />";
$out_string.=$this->fields[$field_name]->draw()."</td>";
}
}
$out_string.= "</tr></table>\n";
return $out_string;
}
This above block of code produces something like this:
I want it such that in the example provided that the word "Transaction" is above the text box.
Please help, the person that programmed this part is indisposed and we've got a deadline.
Thanks for the help.
P.S. The CSS class name for the text is: field_title and the one for the textbox is field_value
Thanks once more.
You will have to debug that code to find out when the label "Transaction" is getting inserted into that cludge of table code. Once you find where "Transaction" is inserted you can then create new logic to add another TR that colspans the table and place the label on that new row.
Good luck, looks like a headache.
Your code is apparently building a rather complex form based on some data ($fields property of the class). I guess some "field settings" (like col) describe how the form should look like.
So, your code is doing things that are neither described in your question nor in the code itself. Furthermore, we have no clue what the complete form currently looks like, so we can't even guess the intention of the code.
Your request, to let the description appear above the selection box(?) could be done probably throwing away half of the code, but that won't help you.
PS: Please check the FAQ - this site is for questions, not for finding people to do your (or elses) work. You should really have a programmer solve your problem directly having access to the whole page.