Asked  7 Months ago    Answers:  5   Viewed   29 times

I am given a page url like 'http://abc.com/test.php?a=1&b=2&c=3'. Now I have been told to change the value of b to 5 so that it becomes 'http://abc.com/test.php?a=1&b=5&c=3'.

i.e change from http://abc.com/test.php?a=1&b=2&c=3 to http://abc.com/test.php?a=1&b=5&c=3

Note: variable b here can refer to any name.

 Answers

70

Use

  • parse_url() to extract the query string from the URL

  • parse_str() to split the query string into an array

  • array_merge() to add a new array "b" => 5

  • http_build_query() to re-build a query string

  • The remaining parts from the first step (protocol, host, path...) to re-build the full URL or - if you have the HTTP pecl extension - a http_build_url() with HTTP_URL_JOIN_QUERY will alleviate much of the work.

Wednesday, March 31, 2021
 
buymypies
answered 7 Months ago
44

Here is how the referred code example supposed to work:

product-category.php

<?php
define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );

$rules = array( 
    'redirect-category'  => "/product-category\.php\?cat_id=(?'category'\d+)"
);

$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode( $uri );

foreach ( $rules as $action => $rule ) {
    if ( preg_match( '#'.$rule.'#', $uri, $params ) ) {
        include( INCLUDE_DIR . $action . '.php' );
        exit();
    }
}

redirect-category.php

<?php
$categories = array(
    2 => 'men-items'
);
header("Location: " . preg_replace( '#'.$rule.'#', "/product_category/{$categories[$params['category']]}/", $uri ));
Saturday, May 29, 2021
 
the_e
answered 5 Months ago
20

You can try something like this:

$test = preg_replace(
    '~/w+/([w-]+)/?type=(w+)~i',
    '/$2/$1/',
    '/technic/k-700/?type=repair'
);
var_dump($test);

The result will be:

string(14) "/repair/k-700/"
Saturday, May 29, 2021
 
simPod
answered 5 Months ago
29

In some cases it is possible to get local variable addresses using HotSpot Serviceability Agent. I've made a sample agent that prints extended stack traces with local variable info:

import sun.jvm.hotspot.code.Location;
import sun.jvm.hotspot.code.LocationValue;
import sun.jvm.hotspot.code.NMethod;
import sun.jvm.hotspot.code.ScopeValue;
import sun.jvm.hotspot.code.VMRegImpl;
import sun.jvm.hotspot.debugger.Address;
import sun.jvm.hotspot.debugger.OopHandle;
import sun.jvm.hotspot.interpreter.OopMapCacheEntry;
import sun.jvm.hotspot.oops.Method;
import sun.jvm.hotspot.oops.Oop;
import sun.jvm.hotspot.runtime.CompiledVFrame;
import sun.jvm.hotspot.runtime.InterpretedVFrame;
import sun.jvm.hotspot.runtime.JavaThread;
import sun.jvm.hotspot.runtime.JavaVFrame;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.runtime.VMReg;
import sun.jvm.hotspot.tools.Tool;

import java.util.List;

public class Frames extends Tool {

    @Override
    public void run() {
        for (JavaThread thread = VM.getVM().getThreads().first(); thread != null; thread = thread.next()) {
            System.out.println(thread.getThreadName() + ", id = " + thread.getOSThread().threadId());
            for (JavaVFrame vf = thread.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
                dumpFrame(vf);
            }
            System.out.println();
        }
    }

    private void dumpFrame(JavaVFrame vf) {
        Method method = vf.getMethod();
        String className = method.getMethodHolder().getName().asString().replace('/', '.');
        String methodName = method.getName().asString() + method.getSignature().asString();
        System.out.println("  # " + className + '.' + methodName + " @ " + vf.getBCI());

        if (vf.isCompiledFrame()) {
            dumpCompiledFrame(((CompiledVFrame) vf));
        } else {
            dumpInterpretedFrame(((InterpretedVFrame) vf));
        }
    }

    private void dumpCompiledFrame(CompiledVFrame vf) {
        if (vf.getScope() == null) {
            return;
        }

        NMethod nm = vf.getCode();
        System.out.println("    * code=[" + nm.codeBegin() + "-" + nm.codeEnd() + "], pc=" + vf.getFrame().getPC());

        List locals = vf.getScope().getLocals();
        for (int i = 0; i < locals.size(); i++) {
            ScopeValue sv = (ScopeValue) locals.get(i);
            if (!sv.isLocation()) continue;

            Location loc = ((LocationValue) sv).getLocation();
            Address addr = null;
            String regName = "";

            if (loc.isRegister()) {
                int reg = loc.getRegisterNumber();
                addr = vf.getRegisterMap().getLocation(new VMReg(reg));
                regName = ":" + VMRegImpl.getRegisterName(reg);
            } else if (loc.isStack() && !loc.isIllegal()) {
                addr = vf.getFrame().getUnextendedSP().addOffsetTo(loc.getStackOffset());
            }

            String value = getValue(addr, loc.getType());
            System.out.println("    [" + i + "] " + addr + regName + " = " + value);
        }
    }

    private void dumpInterpretedFrame(InterpretedVFrame vf) {
        Method method = vf.getMethod();
        int locals = (int) (method.isNative() ? method.getSizeOfParameters() : method.getMaxLocals());
        OopMapCacheEntry oopMask = method.getMaskFor(vf.getBCI());

        for (int i = 0; i < locals; i++) {
            Address addr = vf.getFrame().addressOfInterpreterFrameLocal(i);
            String value = getValue(addr, oopMask.isOop(i) ? Location.Type.OOP : Location.Type.NORMAL);
            System.out.println("    [" + i + "] " + addr + " = " + value);
        }
    }

    private String getValue(Address addr, Location.Type type) {
        if (type == Location.Type.INVALID || addr == null) {
            return "(invalid)";
        } else if (type == Location.Type.OOP) {
            return "(oop) " + getOopName(addr.getOopHandleAt(0));
        } else if (type == Location.Type.NARROWOOP) {
            return "(narrow_oop) " + getOopName(addr.getCompOopHandleAt(0));
        } else if (type == Location.Type.NORMAL) {
            return "(int) 0x" + Integer.toHexString(addr.getJIntAt(0));
        } else {
            return "(" + type + ") 0x" + Long.toHexString(addr.getJLongAt(0));
        }
    }

    private String getOopName(OopHandle hadle) {
        if (hadle == null) {
            return "null";
        }
        Oop oop = VM.getVM().getObjectHeap().newOop(hadle);
        return oop.getKlass().getName().asString();
    }

    public static void main(String[] args) throws Exception {
        new Frames().execute(args);
    }
}

To run it:

java -cp $JAVA_HOME/lib/sa-jdi.jar:. Frames PID

This will attach to Java process PID and print the stacktraces like

main, id = 30920
  # java.lang.Thread.sleep(J)V @ 0
  # Test.main([Ljava/lang/String;)V @ 15
    [0] 0x00007f075a857918 = (oop) [Ljava/lang/String;
    [1] 0x00007f075a857910 = (int) 0x1
    [2] 0x00007f075a857908 = (int) 0x0

Here main is Java thread name; 30920 is native thread ID; @ 15 is bytecode index.

The line [1] 0x00007f075a857910 = (int) 0x1 means that the local variable #1 is located at address 0x00007f075a857910 and has the value 1. This is exactly the variable you are interested in.

The local variable information is reliable for interpreted methods, but not always for compiled methods. However, compiled methods will have an extra line with an address of the code, so you can disassemble and inspect it in gdb.

Sunday, August 15, 2021
 
Arman
answered 2 Months ago
55

What you are asking basically can't be done just like that you have 2 ways about going this route.

  1. Everytime there is an entry retrieval you can run a mongoose hook such as pre('find') that will check if 4 weeks have passed if so then change the deactivated to true you can learn more about hooks on here

Something similar to this example - this example was taken from here

Schema.pre('find', function() {
  if (!this.getQuery().userId) {
    this.error(new Error('Not allowed to query without setting userId'));
  }
});
  1. You can run a cron job every day once or twice (depends on your time period) that keeps checking for records which passed 4 weeks of creation and set deactivated to true

This is a good cron package cron

EDIT: The cron job can be any period, every 10 minutes, 1 hour, 2, 100, etc...

Good luck

Tuesday, August 31, 2021
 
FreedomOfSpeech
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :